1

I have a while loop that loads data from a table row into a div. After one row it loops and loads another row into a duplicate div and so on.

Now I want this loop to select rows at random but have exhausted all rows by the end without duplication.

I have rows:

1
2
3
4
5

I want (random order) rows:

3
2
5
1
4

basically load all rows but in random. I am using PHP.

1
  • 2
    May not even need to pop it off, just shuffle and loop on it once building the html during that one loop. Commented Feb 7, 2018 at 22:08

1 Answer 1

6

Probably easiest to ORDER BY RAND(). You don't show your DB API or functions so replace the function names with what you use:

$result = your_query('SELECT some1, some2 FROM table_name ORDER BY RAND()');

while($row = your_fetch($result)) {
    //use $row in div
}

If you already have your rows in an array, then:

shuffle($rows);

foreach($rows as $row) {
    //use $row in div
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.