开发者

PHP - displaying 1 random record for each week

开发者 https://www.devze.com 2022-12-28 08:37 出处:网络
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 w开发者_如何学Goeek, then select the record and display the new record.

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 w开发者_如何学Goeek, 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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号