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
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
Share on other sites

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'];
}
   
?>

Link to comment
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
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
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
Share on other sites

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.