yoda699 Posted April 9, 2011 Share Posted April 9, 2011 Hi people. I'm building a quiz with one correct answer out of possible 4. My problem is that after I choose a random answer (which is the correct one) I have problems with choosing random 3 wrong answers which are not the same as answer 1 (they all come from the same table). My first mysql query gives me back 1 row my second mysql query gives me back 15 row (there were 16 - 1 which is the correct one). How can take only 3 rows out of that second query and output them into variables that I can later echo into a form? Can you help me understand this problem or can you propose an alternative method to deal with this? Here are my two queries: // Fetch random answer (correct) $query = "SELECT * FROM psychofarm_brand WHERE `id`=".$randomNumber; $result = mysql_query($query); $row = mysql_fetch_array($result); // Fetch wrong answer $query2 = "SELECT * FROM psychofarm_gen WHERE `id`!=".$randomNumber; $result2 = mysql_query($query2); $row2 = mysql_fetch_array($result2); I hope this is clear. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233157-building-a-quiz/ Share on other sites More sharing options...
spiderwell Posted April 9, 2011 Share Posted April 9, 2011 query2 = "SELECT * FROM psychofarm_gen WHERE `id`!=".$randomNumber LIMIT 3;"; is that what you are after? Quote Link to comment https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199064 Share on other sites More sharing options...
yoda699 Posted April 9, 2011 Author Share Posted April 9, 2011 That's a start. My question is this: The array that I get back from that query has 2 columns id name How do I get the 3 rows into 3 separate variables. The only method I know is to echo the rows in a loop: echo $row['name']; I want just variables because I have to randomize the order of all the answers and I the only way I can show the user different order of possible answer is if I have variables that I can play with. I hope this makes my question a bit more clear. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199066 Share on other sites More sharing options...
3raser Posted April 9, 2011 Share Posted April 9, 2011 Not sure if this is what you mean. <?php $query2 = "SELECT * FROM psychofarm_gen WHERE `id`!=".$randomNumber; $result2 = mysql_query($query2); //displays all wrong answers while($row = mysql_fetch_array($result2)) { echo $row['name']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199072 Share on other sites More sharing options...
yoda699 Posted April 9, 2011 Author Share Posted April 9, 2011 That's exactly what I don't want since I can't randomize this output. It will always come in the same order. Is there a way to get $row['name'] into a variable? To extract all the rows just into variables? Quote Link to comment https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199073 Share on other sites More sharing options...
3raser Posted April 9, 2011 Share Posted April 9, 2011 Sorry, your question was a bit confusing there. <?php $query2 = "SELECT * FROM psychofarm_gen WHERE `id`!=".$randomNumber; $result2 = mysql_query($query2); if(!$i) $i = 0; while($row = mysql_fetch_array($result2)) { $i++; $wrong_answer[$i] = $row['name']; } ?> And to display the wrong_answer, say, with the id 2 <?php echo $wrong_answer[2]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199074 Share on other sites More sharing options...
yoda699 Posted April 9, 2011 Author Share Posted April 9, 2011 Great! Thanks, that was what I was looking for. Sorry for the confusion. Quote Link to comment https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199076 Share on other sites More sharing options...
spiderwell Posted April 9, 2011 Share Posted April 9, 2011 <?php $query2 = "SELECT * FROM psychofarm_gen WHERE `id`!=".$randomNumber; $result2 = mysql_query($query2); while($row = mysql_fetch_array($result2)) { $wrong_answer[] = $row['name']; } ?> you dont need the $i counter, it will automatically set the key value when omitted, unless you want to explicitly set it of course Quote Link to comment https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199152 Share on other sites More sharing options...
3raser Posted April 9, 2011 Share Posted April 9, 2011 <?php $query2 = "SELECT * FROM psychofarm_gen WHERE `id`!=".$randomNumber; $result2 = mysql_query($query2); while($row = mysql_fetch_array($result2)) { $wrong_answer[] = $row['name']; } ?> you dont need the $i counter, it will automatically set the key value when omitted, unless you want to explicitly set it of course Ooh, thanks, never knew that. Quote Link to comment https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.