darkwarrior — 2012-05-29T08:44:16-04:00 — #1
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.
starlion — 2012-05-29T10:37:52-04:00 — #2
Or just store a Publish Date in your database, and have your news page query for entries WHERE publish_date < CURRENT_DATE ...
darkwarrior — 2012-05-29T14:31:45-04:00 — #3
Or store a series of events in an array with post_id in the database and have it check that one entry instead?
starlion — 2012-05-29T15:00:21-04:00 — #4
So you're suggesting:
Select Array-Object from Database
Extract Array-Object into Array
Foreach Array as Item
If Item is date-valid
Update news table (or insert)
Select news items
is more efficient than
Select news items with WHERE clause
.... ok. I beg to differ, but do things however you want.
scallioxtx — 2012-05-29T15:10:55-04:00 — #5
QFT. This is by far the easiest and best way to solve this.
darkwarrior — 2012-05-29T16:17:42-04:00 — #6
Well when you say it like that...
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.
starlion — 2012-05-30T08:30:33-04:00 — #7
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...
@r937? @guido2004? Beuller?
darkwarrior — 2012-05-30T08:33:13-04:00 — #8
Right OK, that makes sense.
guido2004 — 2012-05-30T10:04:30-04:00 — #9
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.
kduv — 2012-05-31T01:22:28-04:00 — #10
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.