Portal Home > Knowledgebase > Articles Database > MySQL/PHP - product displayed for set time, then refreshed


MySQL/PHP - product displayed for set time, then refreshed




Posted by LP560, 01-25-2011, 05:24 PM
I'm unsure how to do this. On our website we have data displayed from a MySQL database, every time the page is refreshed it will bring up a random set of data. Rather than this, we would like the data to be displayed so that it refreshes after a set time - say every hour (product displayed for 60mins then refreshed with another, and so on). I'm thinking maybe a timestamp in the database and the query to display the lowest value then update - I don't know, maybe I'm over complicating things? Help and advise much needed and appreciated as always!

Posted by Larnan, 01-25-2011, 06:14 PM
Actually you could kill two birds with one stone here. Instead of checking the database every time to see if you should randomize new data or not, why not cache the full page for one hour? That way, every time somebody visits your site it would follow this sort of procedure: Request > Is cache less than 1 hour old? Yes > Serve cache file No > Randomise data, create a new cache file with the results Get the functionality you're looking for while reducing resource usage!

Posted by ThatScriptGuy, 01-25-2011, 10:19 PM
Or, to extend on Larnan's post - If some of the page needs to be dynamic while this one section needs to be static, then cache just this fragment. This is easily accomplished with most templating engines (If you're using one)

Posted by hgfoo0000, 01-28-2011, 03:22 PM
You could add a new boolean column with a title of "frontpage" or something similar, along with a timestamp to each row. Then your logic could go something to the pseudocode of: 1) select row with 'frontpage' boolean toggle true 2) is row less than an hour old? if not, display it and quit 3) if row is older than an hour, toggle 'frontpage' to false 4) select most recent row, toggle 'frontpage' true, then display it There could be a dozen simpler solutions, but that was the first that came to mind.

Posted by lynxus, 01-28-2011, 03:31 PM
I would probably overcomplicate matters and do this: remove index.php and change to index_gen.php then have a phpscript in crontab set for every hour. When that script runs it could then OUTPUT its output to index.php / html. This way, When people request index.php its a "cached / unchanged" copy. OR!! Id have a script that builds an include file every hour with the stuff you want to display. Every hour it could then "re-generate" that one part of the page for you and make another "include.php" then simply in your index.php use and that way its the ONLY part of the website thats static and only changes every hour on the next "include" update. may be over engineering things here though..

Posted by synrg, 01-28-2011, 03:48 PM
Definitely an a+ for outside of the box thinking but that sounds like quite the complication. What information is being displayed? Is it data results (number of sales for example) or a specific product being sold from 11am to 12pm?

Posted by LP560, 01-30-2011, 10:33 AM
Thanks for the replies - I've tried the cache method, it works but the randomness isn't random enough i.e. it quite often displays the same product rather than switching to another (we only have 4 featured products to display). This is what we are trying to accomplish: We have a featured section on the home page 4 items, 1 displaying every 60 minutes Displays data and one image This is the query we are running at the moment: The above almost does everything apart from the delay and the "randomness" problem.

Posted by coderiser, 02-02-2011, 11:47 AM
you could always break the time string down and place a different image based on the time of day

Posted by coderiser, 02-04-2011, 04:53 PM
you should just reload the page with javascript every X amount of time



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Kayako.com Down? (Views: 659)
Is namecheap.com down? (Views: 688)
Hostech Support Spam (Views: 656)
mail.com down (Views: 659)

Language: