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!
Random Record from DB every day
Moderator: General Moderators
- seodevhead
- Forum Regular
- Posts: 705
- Joined: Sat Oct 08, 2005 8:18 pm
- Location: Windermere, FL
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
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:
I thought of this just now and yes it's awesome.
Only changes once per day.
Code: Select all
SELECT * FROM cars ORDER BY RAND(CURDATE()) LIMIT 1;