www.webdeveloper.com
Results 1 to 3 of 3

Thread: What's the best way of doing this? (Changin a variable once a day)

  1. #1
    Join Date
    Nov 2006
    Posts
    7

    What's the best way of doing this? (Changin a variable once a day)

    Hi all, here's what I'm aiming to do:

    I have an XML file, quotes.xml, which contains several quotes from different famous people.

    What I want to do is pull in one quote at a time. I've no problems doing this - it's working perfectly with a random quote being pulled into a web page each time.

    However, I need the page to 'cycle' through the list of quote entries in the XML. I.e., one day it will use the first quote, then after 24 hours it will use the second quote, 24 hours after that it will use the third quote, etc. When it reaches the end it will start from the beginning again.

    The method I've thought of was in creating a file on the server which stores something like $lastQuote = 6; and somehow checks this every day, changes it to 7 and starts using quote number 7.

    This seems like it may be unnecessary though, and surely opening and reading a file every time the page is loaded would use many server resources? Can someone suggest the best way of going about doing this?

    Many thanks in advance

  2. #2
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    PHP Code:
    $num 30// number of quotes that exist
    $day date('z'); // the day of the year (starting from 0)
    $i 0// base integer

    while($day $num)
        
    $day -= $num;

    // now retrieve quote number via the day variable
    ?> 

  3. #3
    Join Date
    Nov 2006
    Posts
    528
    Here's a very easy, one-line solution:

    Code:
    $todaysQuote = $quoteList[date("z") % count($quoteList)]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles