Jump to content

Order by Rand() Limit question


realjumper

Recommended Posts

Hi,

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

[code]
// 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]";
[/code]

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

[code]
// 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 ); 
[/code]

...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. :)
Link to comment
https://forums.phpfreaks.com/topic/13794-order-by-rand-limit-question/
Share on other sites

::) 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!?!?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.