tags:

views:

58

answers:

3

I want to display 1 random record from a database based on the week.

I need to determine if it's a new, and if it is a new week, then select the record and display the new record.

I'm thinking I can just use a single day of the week to generate the new record, either way will work.

I'm really having a hard time conceptualizing how I'll store the record id and not select a new one when someone visits again the same day or refreshes the page.

Any ideas? Let me know if I wasn't clear enough.

Also, I don't have access to cron jobs as it's shared hosting.

+2  A: 

Create a table containing the id of the row you want to display and a datetime/timestamp column that says when that row was inserted. Then all you have to do is to update the id if it is more than week since it was inserted. Unfortunately you would have to do this check for every request. If you don't want that you could create a cron-job that runs once a week and updates the table.

klausbyskov
I get what you're saying, but am I only inserting one record or a new one each time a new record is selected?
mike
@mike I would go with just one record in the table... unless you want to keep a history of what you have been displaying in the past.
klausbyskov
Alright thanks! I think I should be figure this out now.
mike
A: 

Have a script select the random recored from your database then place that recored into a space on your database. Then have the script that displays the recored point to the location on your database. That way selecting the script would be run only once as a cron job.

Basically script A pulls the recored and replaces the newly selected file over the old one then script B is pointing to that tables entry. If you can't create tables and entries just save in a text file the Id of the recored from script A and then read that Id into the script that is displaying it.

Doodle
A: 

MySQL's function RAND() accepts an integer parameter N, which is used as a seed value. This allows to get the same sequence in multiple requests - you may take a look at examples in http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand. I would try using something like SELECT field_1, field_2, field_3 FROM table_name ORDER BY RAND($time) ASC LIMIT 1, where $time would be a UNIX-timestamp for the beginning of current week.

binaryLV