Jump to content

Building a quiz


yoda699

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/233157-building-a-quiz/
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199066
Share on other sites

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];

?>

Link to comment
https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199074
Share on other sites

 

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

Link to comment
https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199152
Share on other sites

 

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

Link to comment
https://forums.phpfreaks.com/topic/233157-building-a-quiz/#findComment-1199157
Share on other sites

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.