Jump to content


Photo

Counting up correct answers in a quiz - help please.


  • Please log in to reply
9 replies to this topic

#1 gerrydewar

gerrydewar
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 08 March 2006 - 09:02 PM

Hello everyone,

Hope someone can help me out with my code here. I'm creating a multiple choice quiz and i have so far managed to generate random questions from my database and have them display on screen in a nice neat way. Each answer has a radio button so the user can select an answer. So as you can see from my script below i display the form before the submit button is pressed. As soon as the submit is pressed i would like the total number of correct answers to be shown. This is the bit of my script that is confusing me. I currently get an error message telling me that it doesn't like my 'mysql_fetch_array' just below the $score = 0 line. I think i'm quite close to solving this problem. Does anyone have any ideas?

<body>
<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){

$score = 0;
while($row = mysql_fetch_array($result,MYSQL_NUM)){

$answer = $row[2];

if ($guess == $answer) {
$score++;
}else{
$score = $score;
}
}
echo '<p><b> you scored $score</b></p>';

}else{//Display the form.

?>

<form name ="systemstest" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<div align="center"><input type="submit" value="check score" name="submitted"></div>
<?php

//Connect to db.
require_once ('../mysql_connect.php');

//Select 5 random questions from database
$query = "SELECT question_id, question, answer, choice1, choice2, choice3 FROM questions WHERE subject_id = 1 ORDER BY RAND() LIMIT 5";
$result = @mysql_query ($query); //Run query

if($result) {
echo '<table align="left" cellspacing="10" cellpadding="10">';

while ($row=mysql_fetch_array($result,MYSQL_NUM)){

$id = $row[0];
$questions = $row[1];
$answer = $row[2];
$opt1 = $row[3];
$opt2 = $row[4];
$opt3 = $row[5];

echo "<tr><td align=\"left\"><li>$questions</td></tr>
<tr> <td align=\"left\"><input type=\"radio\" name=\"guess\"value=\"$opt1\"/>$opt1</td></tr>
<tr> <td align=\"left\"><input type=\"radio\" name=\"guess\"value=\"$opt2\"/>$opt2</td></tr>
<tr> <td align=\"left\"><input type=\"radio\" name=\"guess\"value=\"$opt3\"/>$opt3</td></tr>
\n";
}
echo '</table>';
//echo '<table>';

}else{
echo'<p>Test failed</p>';
}

mysql_close();
}
?>
</form>
</body>

#2 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 08 March 2006 - 09:42 PM

Just looking code over, what's the MYSQL_NUM for?

also, can you give the error, please?

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 March 2006 - 09:42 PM

When you press submit, your script is started again, so you have to do all of the preliminary database stuff again.

Also instead of
<?php if($_SERVER['REQUEST_METHOD'] == 'POST') ?>
you probably want to use
<?php if (isset($_POST['submitted'])) ?>


Ken

#4 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 08 March 2006 - 09:50 PM

I cut some fat... See my other remarks at the end...
<body>
  <?php

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $score = 0;
    while($row = mysql_fetch_array($result,MYSQL_NUM)){
    $answer = $row[2];
        if ($guess == $answer) {
        $score++;
        }
        }
    }
    echo '<p><b> you scored '.$score.'</b></p>';

    }
<snip>
I don't use PHP_SELF so I don't really know the answer for sure, but I don't know if you can cycle through a result twice... I would have made the answers post hidden with the responces in the form... That seems easier to me...

#5 gerrydewar

gerrydewar
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 08 March 2006 - 09:57 PM

[!--quoteo(post=353019:date=Mar 8 2006, 09:42 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 8 2006, 09:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Just looking code over, what's the MYSQL_NUM for?

also, can you give the error, please?
[/quote]

I use the MYSQL_NUM for the array as my database info gets stuck into an array. The error i am getting is

Warning: mysql_fetch_array():supplied argument is not a valid MySQL result resource in c:\.....on line ....


#6 gerrydewar

gerrydewar
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 08 March 2006 - 10:11 PM

[!--quoteo(post=353020:date=Mar 8 2006, 09:42 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 8 2006, 09:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
When you press submit, your script is started again, so you have to do all of the preliminary database stuff again.

Also instead of
<?php if($_SERVER['REQUEST_METHOD'] == 'POST') ?>
you probably want to use
<?php if (isset($_POST['submitted'])) ?>
Ken
[/quote]

I'll give that a go. How much of the preliminary database stuff? Just

//Connect to db.
require_once ('../mysql_connect.php');

or anymore?

#7 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 09 March 2006 - 02:07 AM

Again, if you put the answer in <input type=hidden> along with the responces, you wouldn't need to run through it again...

#8 gerrydewar

gerrydewar
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 09 March 2006 - 10:14 PM

[!--quoteo(post=353108:date=Mar 9 2006, 02:07 AM:name=AV1611)--][div class=\'quotetop\']QUOTE(AV1611 @ Mar 9 2006, 02:07 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Again, if you put the answer in <input type=hidden> along with the responces, you wouldn't need to run through it again...
[/quote]

Here is what i have now. It gives an answer of 0 all the time so far. All i want to be able to do is compare the selected radio button value with the correct answer. If it is correct then add one to total. How hard can that be? Bloody hard in my case!!!!

<body>

<form name ="systemstest" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<div align="center"><input type="submit" value="check score" name="submitted"></div>
<?php

//Connect to db.
require_once ('../mysql_connect.php');

//Select 5 random questions from database
$query = "SELECT question_id, question, answer, choice1, choice2, choice3 FROM questions WHERE subject_id = 1 ORDER BY RAND() LIMIT 5";
$result = @mysql_query ($query); //Run query

if(!isset($_POST['submitted'])){ //display the form

if($result) {
echo '<table align="left" cellspacing="10" cellpadding="10">';

while ($row=mysql_fetch_array($result,MYSQL_NUM)){

$id = $row[0];
$questions = $row[1];
$answer = $row[2];
$opt1 = $row[3];
$opt2 = $row[4];
$opt3 = $row[5];

echo "<tr><td align=\"left\"><li>$questions</td></tr>
<tr> <td align=\"left\"><input type=\"radio\" name=\"$id\" value=\"$opt1\"/>$opt1</td></tr>
<tr> <td align=\"left\"><input type=\"radio\" name=\"$id\" value=\"$opt2\"/>$opt2</td></tr>
<tr> <td align=\"left\"><input type=\"radio\" name=\"$id\" value=\"$opt3\"/>$opt3</td></tr>
<tr> <td align=\"left\"><input type=\"hidden\"name=\"rightanswer\" value=\"$answer\"/></td></tr>
\n";
}
echo '</table>';
echo '</form>';

}else{
echo'<p>Test failed</p>';
}
}else{
$score=0;

if($_POST['$id']==$_POST['rightanswer']){
$score++;
}

echo 'you scored '.$score.'';

mysql_close();
}
?>
</body>
</html>


#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 March 2006 - 10:54 PM

Are you trying to make the names of the radiobuttons the same as your the contents of your varible "$ID"? If so, your script is wrong. Try this instead:

<?php
echo '<tr><td align="left"><li>' . $questions . '</td></tr>
<tr> <td align="left"><input type="radio" name="choice[' . $id . ']" value="' . $opt1 .'"/>' . $opt1 . '</td></tr>
<tr> <td align="left"><input type="radio" name="choice[' . $id . ']" value="' . $opt2 .'"/>' . $opt2 . '</td></tr>
<tr> <td align="left"><input type="radio" name="choice[' . $id . ']" value="' . $opt3 .'"/>' . $opt3 . '</td></tr>
<tr> <td align="left"><input type="hidden" name="rightanswer[' . $id . ']" value="' . $answer . '"/></td></tr>' . "\n";
}?>

Then in your processing code:
<?php
$score = 0;

foreach($_POST['choice'] as $id => $ans)
     if($_POST['rightanswer'][$id] == $ans) $score++;

?>

The way you have it scripted all of your radio buttons were named "$id" so if you had more than one question only the last choices would be returned. The same with the right answer.

Ken

#10 gerrydewar

gerrydewar
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 10 March 2006 - 12:31 AM

Worked a treat, thanks Ken.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users