SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A quote a day...

    I've got a random quote generator, which I'm more than happy with - but I can't seem to figure out how to make it appear once a day rather than just randomly.

    Does it rely on the date field? Any help on this would be appreciated.

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might help if we could see the code for the quote generator?

  3. #3
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $query = "SELECT * FROM quotes ORDER BY RAND(now()) LIMIT 1";
    The above code randomly selects a quote - so far so good, but this'll change it for every time you refresh the page - is there a way to specifiy to change the quote once a day only?

  4. #4
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just get it to write the quote to a text file along with the date. Then once the date has gone past - re-write a new quote!

  5. #5
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another possibility could be:
    add a date/timestamp to your table.
    SELECT for a quote of the current date.
    If it exists, use it if not, choose a random quote and timestamp it to the current day.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  6. #6
    SitePoint Zealot tcwatts's Avatar
    Join Date
    Jan 2003
    Location
    .my
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Here's something I did awhile back - quick 'n dirty but does the job.

    $dayofyear=(date(z));

    $sql = "SELECT field FROM table WHERE id = $dayofyear";
    $result = mysql_query($sql) or mysql_die();
    $row = mysql_fetch_array($result);

    echo $row[field];

    Then of course you should have 365 rows in your db. The 'z' in the date function gives you the day of the year. You will have a shift for leap year, but i hope this is enough to give you an idea.

  7. #7
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bleh..you're making it out to be too complicated! Just set a quick cron job and go with plat's idea.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  8. #8
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My host unfortuently doesn't allow cron jobs unless I upgrade, which I'm not in the financial position to do about now.

    Thanks for all your help. I like the look of tcwatts concept th emost - it gives 365 quotes, so that seems more than enough.

    I'm confused by frenzo's comments...

    [quote]
    If it exists, use it if not, choose a random quote and timestamp it to the current day.
    [/code]

    On the first run and there's no quote assigned - it chooses a random quote and timestamps it to the current day - but what happens when you run it again? Doesn't it just find another random quote?

  9. #9
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by worchyld
    I'm confused by frenzo's comments...
    hmmm, i'm trying to confuse girls, and that usually doesn't work.
    now i'm confusing men, and that's confusing me

    ok, let's try it again:

    1) First of all you an a date timestamp to your quotes table.

    2) If a random quote should be displayed, you first check your whether one of your quotes is timestamped from today. If yes, you use this quote.

    3) If no quote has a timestamp with the date of today, that means that there was no 'quote of the day' yet today. So you select a random quote for displaying and give this quote todays timestamp.
    Now you have just this quote for the day, since with checking the timestamp of your quotes (see point 2) this quote is handled as todays quote of the day.

    Hope you could follow me.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  10. #10
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that makes sense - sort of. But wouldn't the date timestamp be empty. Ah, stuff it, I'll keep it random..

  11. #11
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by worchyld
    Yes, that makes sense - sort of. But wouldn't the date timestamp be empty. Ah, stuff it, I'll keep it random..
    I think what he's trying to say is this:

    Try to select a quote with today's timestamp
    if no timestamp for today
    select one at random, insert today's timestamp to this field.

    Next time it loads *today* it should select the same one, tomorrow there won't be one with a timestamp and it will grab one by random, assign it a timestamp and the rest of the day it'll grab the same one.
    Chrispian H. Burks
    Nothing To Say

  12. #12
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Crowe
    I think what he's trying to say is this:
    you got it
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  13. #13
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by worchyld
    My host unfortuently doesn't allow cron jobs unless I upgrade, which I'm not in the financial position to do about now.
    You may want to take a look at WebCron.

    FWIW, any host that charges for stuff like cron jobs is penny-pinching you, even if they're a budget host. I'd look at switching; chances are you can get a more reasonable deal for the same price.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  14. #14
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about something like this:

    PHP Code:
    $today date ('dmY');
    srand ($today);
    $min 1// make this the smallest quote ID 
    $max 20// make this the largest quote ID
    $ID =  rand($min$max); 
    Rand gets seeded by srand using the date, will produce the same number until the day changes. I bet some variation on that could work.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  15. #15
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sam, that's pretty nice. You could always just use select by RAND with that too, right?
    Chrispian H. Burks
    Nothing To Say

  16. #16
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Crowe
    Sam, that's pretty nice. You could always just use select by RAND with that too, right?
    Yes, you are right, and that would be a superior way to do it. It would be something like:

    SELECT * from TableX WHERE ID=ROUND( RAND(CURDATE()) * maximum value here)

    A database that supports subselects (like MySQL 4.1, still in Alpha) could figure out that maximum number too in one query. I looked through the MySQL documentation and couldn't find a good way to do that perhaps someone else knows it.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  17. #17
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  18. #18
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    Yes, you are right, and that would be a superior way to do it. It would be something like:

    SELECT * from TableX WHERE ID=ROUND( RAND(CURDATE()) * maximum value here)

    A database that supports subselects (like MySQL 4.1, still in Alpha) could figure out that maximum number too in one query. I looked through the MySQL documentation and couldn't find a good way to do that perhaps someone else knows it.
    Great work Sam! Very nice solution...


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
  •