I tried to use ORDER BY rand() to randomize the data fetched from MySQL table in my php website. But, the problem is, after I go to next page, some data from page 1 appear in page 2 and so on. I tried searching the web for the php version of randomization. But, I can't find it. Could anyone help me in this problem? Thank you :D
5 Answers
You can do it by still using the SQL-rand().
Instead of calling rand() empty, call it with a seed, which stays the same. Randomly generate that seed in php.
For example, ORDER BY rand(5) will always return the same order.
If you are using Ajax calls to load other pages, submit the same seed on every call. If it's a page reload on every page switch, you can send it with GET/POST for it to stay the same.
Comments
I suppose that you use LIMIT x,y in your query for make a paging.
So, one possible solution is to use a SEED in your ORDER BY and pass it with SESSION or via GET
example:
$seed = isset($_SESSION['seed'])? $_SESSION['seed'] : mt_rand();
$_SESSION['seed'] = $seed;
$qry = "SELECT * FROM `table` ORDER BY RAND($seed) LIMIT 0,5";