Jump to content

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!?!?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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