I’m developing a small PHP CMS and I want to be able to schedule news items and the like for future dates and automatically post them when that date is due. Would I just include a check script in my header whenever a page is loaded to check for scheduled posts in the database and set them to publish now?
That’s the only way I can think of doing it but that seems like it would be somewhat resource intensive.
Wouldn’t having to search through all the posts each load be inefficient though? I can’t claim to understand how PHP and SQL loads work. I guess you’d have to keep a timer somewhere to prevent the process running more than once a minute.
Well the Database guys could weigh in on this a lot better than I can, but… no. Inefficient would be 4 (or 5) queries instead of 1. (SELECT, UPDATE or [INSERT,DELETE], SELECT, UPDATE vs SELECT). The ‘extra load’ of the database engine having to do a date-based result-restriction would not equate to the processing calls for multiple queries…
It seems like a normal query to me. And certainly more efficient than the array solution.
If you’re expecting so much traffic that it might become too big a burden on your server then you might want to look into a catching system.
Or, you could just set up a MySQL event (similar to a cron) to do it. You wouldn’t even need to use PHP. Just set the event to run once a day and change published = 0 to published = 1 for articles matching the timestamp specifications.