tags:

views:

511

answers:

2

I have a table of product. I have listed the items with pagination using rand() function. When using rand(), it displays the items in random order but the record is repeated while we go from 1 page to another page. Is it possible to get non repeated items in each page using rand()? If yes how, if no what may be the option for this.

+3  A: 

Well, you could generate a random seed when doing the first query (in your app code), and then keep that seed between requests - and supply this seed to the rand([seed]) function. That should do it...

Of course, the specifics would depend on how exactly your sql code is working at the moment; you might need to loop to comsume the first page (or so) of random numbers to ignore.

Marc Gravell
Thank you so much Marc for your suggestion...... I tried to find out how to generate random seed but could not find exactly what to do ... Here is the sql query (select * from product rand() limit 0,10). So if possible can you tell me how to generate random seed and supply to rand(). Thanks again
A: 

In a situation like that, I might see if there's a way to create a list of the product IDs (I'm assuming each product has some sort of unique ID) and shuffle it. The list might be stored as a temporary database table or it might be stored as a session variable in your web application, for example... that way you could get access to any part of the randomly ordered list without having to recompute the whole thing from the beginning.

This is obviously best if you have few people accessing the site at any given time, but you expect each of them to go through several pages of the results.

David Zaslavsky
Thank you David for your answer. I solved the problem using session variable. What I want to know is that do you mean it is not good when many people access page at the same time.
I mean that the method I described is most useful if not too many people are accessing the site at any given time. You would need to store a random order of products for each person, and if you have a lot of visitors simultaneously that adds up to a lot of memory.
David Zaslavsky
Thanks again Devid............