0

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

0

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 ;).

Comments

0

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";

Comments

0

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.

Comments

0

It seems that you are searching for a function like shuffle() in php. Store the returned data in an array and use shuffle(array)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.