Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

how to shuffle contents fetched from database before printing


This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 alamin


    CC Newcomer

  • Member
  • PipPip
  • 10 posts

Posted 13 February 2013 - 06:25 AM

hey pals am kind of new here. i am developing an online exam system using php,html and mysql and i need a help on how to shuffle the questions fetched from the database for each user. Pls help

#2 BlackRabbit


    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 13 February 2013 - 06:49 AM

Welcome aboard Alamin,

I suggest you just do your regular rows fetch and then make an int array of equal items count than the rows.

fill the int array with random numbers from 0 to array length.


Then, when you want to get the question number one, you go to the int's array, which has the rows indexes shuffled. get the value of position one and use that value as the index for the rows array.

#3 gregwarner


    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 13 February 2013 - 06:49 AM

You can SELECT the rows from the table in random order with the following SQL query:
That's the simplest way. Otherwise, you'd have to write a shuffle algorithm in PHP, which is not difficult, and you can learn just by Googling "shuffle algorithm".

Edited by gregwarner, 13 February 2013 - 06:50 AM.

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download