SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Schedule the future publishing of items?

    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.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Or just store a Publish Date in your database, and have your news page query for entries WHERE publish_date < CURRENT_DATE ...
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or store a series of events in an array with post_id in the database and have it check that one entry instead?

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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)
    endif
    endforeach
    Select news items

    is more efficient than

    Select news items with WHERE clause

    .... ok. I beg to differ, but do things however you want.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,891
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by StarLion View Post
    Or just store a Publish Date in your database, and have your news page query for entries WHERE publish_date < CURRENT_DATE ...
    QFT. This is by far the easiest and best way to solve this.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    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)
    endif
    endforeach
    Select news items

    is more efficient than

    Select news items with WHERE clause

    .... ok. I beg to differ, but do things however you want.
    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.

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by darkwarrior View Post
    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...

    @r937 ? @guido2004 ? Beuller?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right OK, that makes sense.

  9. #9
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,398
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    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.

  10. #10
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    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.

    See: http://dev.mysql.com/doc/refman/5.1/...ate-event.html


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •