Reputation: 51
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
Upvotes: 0
Views: 90
Reputation: 258
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.
Upvotes: 1
Reputation: 56
It seems that you are searching for a function like shuffle() in php. Store the returned data in an array and use shuffle(array)
Upvotes: 0
Reputation: 108
Why would you make pagination on random data?
But...if you still want to do it, you could move from page to page using POST request. But this is still something unusable.
Upvotes: 0
Reputation: 16458
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";
Upvotes: 0
Reputation: 1979
If you calculate the random number everytime, it's clear that you'll have the same values on other pages, too. You need to get the random number once and pass it to the other sites, eg. per HTTP-Get.
With some code, someone could help you more ;).
Upvotes: 0