tags:

views:

108

answers:

7

I have about 10,000 products in the product table. I want to retrieve one of those items and display it in a section of a web page which stays the same for that particular day. Something like "Product of the day".

For example, if today I get product_id 100, then all of the visitors should be viewing this product item for today. Tomorrow it may fetch any random valid primary key, say, 1289 and visitors get 1289 product all day tomorrow.

Any ideas/suggestions?

Thanks for your help.

+2  A: 

How about create a cache file and invalidate it at midnight?

The benefit of this is you don't make unnecessary calls to your DB as you're only checking the timestamp on the cache file - only once per day do you make DB requests to populate a new cache file.

You don't need a CRON job for this:

if(date_of_file(potd_cache_file) != today){
  potd_cache_file = generate_from_db();
}
load_file(potd_cache_file);

This will mean only the first visitor of the day to your website will trigger the regeneration, and every subsequent visitor will have a fast loading cache file served to them.

Rew
I am sorry .. may be I was not very clear in my question. I want it to display in a section of a webpage.. something like item of the day.
Wbdvlpr
so other items may keep changing but this one stays same for that day.
Wbdvlpr
If it can't be stored in the database, I would do this too I think. Have a single writable file with the id in it. Then when the page is viewed, check the modification date and if it was yesterday, get a new id and store it in the file, otherwise use the id in the file. Date could also be stored in the file of course instead of checking the modification date.
Svish
You don't have to cache the entire HTML file, you can cache the portion that remains static for the day.
Rew
@Svish, Date wouldn't need to be stored in the file, you could just check the last modification date of the file. If it's PHP you could check the last modification date and, if it's today, include the file. If it's older than today, fetch the product from the database and write it to the file.
Andy E
Actually this is similar to what I implemented to this site: http://www.fantasymotogp.co.uk all the tables you see are cached as the data doesn't change very often at all, it works a treat, the traffic to my DB is very low, even when the site traffic is high.
Rew
+2  A: 

Maybe you can store the id of the item of the day in a table in the database?

Lex
Yes, I can. But I need somebody (an admin) doing this and change it everyday.
Wbdvlpr
Not really. You just make code "If exists randomitemfortoday use, else pick"
Russell Steen
Alternatively, setup a cron job to pick one random item every day and store it in the table.
Lukáš Lalinský
Also If the item goes out of stock I'll have to update this table as well. So I'd prefer to keep it simple .. and I find @Quassnoi's answer working fine for me.
Wbdvlpr
+3  A: 
SELECT  id
FROM    products
ORDER BY
        RAND(UNIX_TIMESTAMP(CURRENT_DATE()))
LIMIT 1
Quassnoi
Looks like working for me. Let me test it. Thanks.
Wbdvlpr
+1  A: 
SELECT  id
FROM    products
ORDER BY (id + RAND(UNIX_TIMESTAMP(CURRENT_DATE()))) MOD some_reasonable_value
LIMIT 1
erikkallen
+2  A: 

The idea is pretty simple,

  1. Set a table up call ProductOfTheDay with a product ID and a date field
  2. On the product of the day page when a user visits check the date field
  3. If it is todays date then show the product
  4. If it is not then randonly pick a new product and save it to the field.

Its not that complex of an operation.

Pino
+1, I like this, it gives you the benefit of having a history of 'product of the day'.
Rew
A: 

You can start random number generators with a seed value.

Make the seed value be the day (21st) + month(10) + year(2009) so today's seed is 2041.

You will get the same random number all day, and tomorrow a different one. This is more how it works in .net. The random function takes a max and min value (this is your min and max ID values) then an optional seed value and returns a number. For the same seed number you get the same random number generated. It's possible if you change the max and min this can affect the number generated. You would have to look up how php works.

Robert
A: 
total = SELECT COUNT(id) FROM products;
day_product = SELECT id FROM products WHERE id = (UNIX_TIMESTAMP(CURRENT_DATE()) MOD total) LIMIT 1;

See also this question.

Alix Axel
What if the `id`'s are sparse?
Quassnoi
They should never be in a ecommerce application, they should have a deleted flag instead of being actually deleted.
Alix Axel