Random Record from DB every day

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Random Record from DB every day

Post by seodevhead »

Hey guys. I have a section on my website that has a "Today's Featured Car" spot where everyday I update it to feature a random car in my database. I want to make this 'automatic' and write a php snippet to randomly select one of the car records each day and display that one random car for 24 hrs.

I know how to write it so it can randomly select a record from the database, but the problem is it selects a random record on every page reload. How can I go about having it automatically select a record each day and use that same record throughout the entire 24 hrs. Thanks for any advice and help!
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Hummm, One way is to have a text file 'todays_car' , with the name of the car in it and have a daily cron job which executes a tiny php script that selects a random car from the database and updates the text file.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

You'll probably need to create another table.

Then, when you have selected a random record, insert which record and the time it was selected in the new table.

then, on each page load, just check to see if 24 hours has passed since the time stored int he new table.

if not, select the existing "featured" record, otherwise, sleect a new random record, and repeat the process again
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

I thought of this just now and yes it's awesome.

Code: Select all

SELECT * FROM cars ORDER BY RAND(CURDATE()) LIMIT 1;
Only changes once per day.
Post Reply