Page 1 of 1

Random Record from DB every day

Posted: Thu Jun 29, 2006 9:51 am
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!

Posted: Thu Jun 29, 2006 9:57 am
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.

Posted: Thu Jun 29, 2006 9:59 am
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

Posted: Fri Jun 30, 2006 12:40 pm
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.