Jump to content


Photo

Order by Rand() Limit question


  • Please log in to reply
1 reply to this topic

#1 realjumper

realjumper
  • Members
  • PipPipPip
  • Advanced Member
  • 399 posts

Posted 05 July 2006 - 10:15 PM

Hi,

From a select query I get returned a single record, which is fine.....as below:

// Retrieve all the data from the table
$result = mysql_query("SELECT question FROM kanji.sentence WHERE id = '$id'")

// store the record of the table into $row          
$row = mysql_fetch_array( $result );  

echo "$row[question]";

Now, in the sentence table there are 6 possible answers to the question posed , and only one of them is correct. 'Item_1' is always the correct answer. What I want to do is to display the correct answer and 3 incorrect answers in a random order from which the user can choose. I can display four possible answers randomly as below.....

// Retrieve all the data from the table
$result2 = mysql_query("SELECT Item_1,Item_2,Item_3,Item_4,Item_5,Item_6 FROM kanji.sentence WHERE sentence = $row[sentence] ORDER BY RAND() LIMIT 0,4")

// store the record of the table into $row          
$row2 = mysql_fetch_array( $result );  

...of course the above doesn't always return the correct answer (Item_1) with another 3 possible answers, it just returns a random 4 answers. How can I always return 'Item_1' plus any 3 of the remaining Items (Item_2, Item_3, Item_4, Item_5, Item_6) in a random order?

Thanks. :)

#2 realjumper

realjumper
  • Members
  • PipPipPip
  • Advanced Member
  • 399 posts

Posted 05 July 2006 - 11:55 PM

::) What if in the first query I also pull 'Item_1' out along with 'question'....and then in the second query I pull out 'Item_2,Item_3,Item_4,Item_5,Item_6' order by rand() limit 0,3. Then I could put 'Item_1' and the other 3 items (from query 2) into an array and use 'shuffle()' to randomize the output!!

Does that sound like a plan or does it sound silly!?!?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users