Jump to content

Recommended Posts

I am creating an exam and for some reason when I get all the questions right it still says 33 or 0 here is my code

 

 

exam.php

 

<?php
include(db.php");
?>


<?php



$elist = mysql_query("SELECT * FROM `entrance_exam` ORDER BY rand()");

$elrows = mysql_num_rows($elist);

if ($elrows > '0'){
?>

<table width="100%">
<?php 

while ($elr = mysql_fetch_array($elist)){

$counter = $counter + 1;

?>
<form action="?page=correct_exam" method="post"/>
<?php

echo "<tr bgcolor=#4c7cb2><td><font color=white><b>$counter. $elr[Question]</b></font></tr>";

echo " <tr bgcolor=#EEEEEE><td>A:<input type=radio name=$elr[id] value=a> $elr[A]</td></tr>";

echo "<tr bgcolor=#EEEEEE><td>B:<input type=radio name=$elr[id] value=b> $elr[b]</td></tr>";

if ($elr[C]){ echo " <tr bgcolor=#EEEEEE><td>C:<input type=radio name=$elr[id] value=c> $elr[C]</td></tr>"; }

if ($elr[D]){ echo " <tr bgcolor=#EEEEEE><td>D:<input type=radio name=$elr[id] value=d> $elr[D]</td></tr>"; }



}



echo "<tr><td><input type=submit value=\"Submit Exam\"></td></tr>";

echo "<input type=hidden name=now value=$now>";

echo "<input type=hidden name=viewexam value=yes>";
} else { echo'No Questions in database'; }
?>

</table></form>

 

correct_exam.php

<?php
include("db.php");

$a1 = $_POST[1];

$a2 = $_POST[2];

$a3 = $_POST[3];





if ($a1 == 'b'){ $correct = $correct + 1; }

if ($a2 == 'b'){ $correct = $correct + 1; }

if ($a3 == 'a'){ $correct = $correct + 1; }



$s1 = $correct / 3;

$s2 = $correct * 100;

$s3 = round($correct,0);




?>


Your socre was <?php echo"$s3";?>

 

Link to comment
https://forums.phpfreaks.com/topic/166878-need-help-please/
Share on other sites

Your logic is wrong..

 

If all 3 answers are correct then $correct should be 3

 

$s1 = $correct / 3;

 

> $s1 = 3 / 3 , so 1

 

$s2 = $correct * 100;

 

> $s2 = 3 * 100, so 300

 

$s3 = round($correct,0);

 

> $s3 = round(3,0);

 

so by your logic $s3 would be 3

 

This is the string you should use instead.

 

<?php

include("db.php");

$a1 = $_POST[1];
$a2 = $_POST[2];
$a3 = $_POST[3];

$correct = 0; # Initialize the Variable

if ($a1 == 'b'){ $correct = $correct + 1; }

if ($a2 == 'b'){ $correct = $correct + 1; }

if ($a3 == 'a'){ $correct = $correct + 1; }

$score = round((($correct / 3) * 100),0);

echo "Your score was $score";

?>

Link to comment
https://forums.phpfreaks.com/topic/166878-need-help-please/#findComment-879925
Share on other sites

$correct = 0; # Initialize the Variable
echo 'Initialised $correct to '.$correct.'<br /><br />';

echo '$a1 = '.$a1.'<br />';
if ($a1 == 'b'){ $correct = $correct + 1; 
   echo '$correct incremented to '.$correct.'<br />';
}

echo '$a2 = '.$a2.'<br />';
if ($a2 == 'b'){ $correct = $correct + 1; 
   echo '$correct incremented to '.$correct.'<br />';
}

echo '$a3 = '.$a3.'<br />';
if ($a3 == 'a'){ $correct = $correct + 1; 
   echo '$correct incremented to '.$correct.'<br />';
}

echo '<br />$correct / 3 => '.$correct.' / 3 => '.($correct / 3).'<br />';
echo '($correct / 3 ) * 100 => ('.$correct.' / 3) * 100 => '.(($correct / 3) * 100).'<br /><br />';
$score = round((($correct / 3) * 100),0);

Link to comment
https://forums.phpfreaks.com/topic/166878-need-help-please/#findComment-880436
Share on other sites

I want it to add up all the correct ones and for every correct one add 1pt to the score and then display the score. How can I do that.

 

 

Mark Baker

 

When I use your code I received the following:

 

 

Initialised $correct to 0

 

$a1 = b

$correct incremented to 1

$a2 =

$a3 =

 

1 / 3 => '.1.' / 3 => '.(1 / 3).'

(1 / 3 ) * 100 => ('.1.' / 3) * 100 => '.((1 / 3) * 100).'

Link to comment
https://forums.phpfreaks.com/topic/166878-need-help-please/#findComment-880484
Share on other sites

When I use your code I received the following:
Well, aside from a couple of errors in my echo statements, that's telling you that there are no values for $a2 and $a3, so they never match your expected 'b' and 'a' respectively.

 

Double check your POST vars

Link to comment
https://forums.phpfreaks.com/topic/166878-need-help-please/#findComment-880493
Share on other sites

This is my form here

 

 

echo "<tr bgcolor=#4c7cb2><td><font color=white><b>$counter. $elr[Question]</b></font></tr>";

 

echo " <tr bgcolor=#EEEEEE><td>A:<input type=radio name=$elr[id] value=a> $elr[A]</td></tr>";

 

echo "<tr bgcolor=#EEEEEE><td>B:<input type=radio name=$elr[id] value=b> $elr</td></tr>";

 

if ($elr[C]){ echo " <tr bgcolor=#EEEEEE><td>C:<input type=radio name=$elr[id] value=c> $elr[C]</td></tr>"; }

 

if ($elr[D]){ echo " <tr bgcolor=#EEEEEE><td>D:<input type=radio name=$elr[id] value=d> $elr[D]</td></tr>"; }

 

and basically it's just a loop to get all the questions out of the database in a random order. Also the name is a number  (i.e name=1)

Link to comment
https://forums.phpfreaks.com/topic/166878-need-help-please/#findComment-880497
Share on other sites

When I use your code I received the following:
Well, aside from a couple of errors in my echo statements, that's telling you that there are no values for $a2 and $a3, so they never match your expected 'b' and 'a' respectively.

 

Double check your POST vars

 

Did you checked your POST vars names... check the names again.

Link to comment
https://forums.phpfreaks.com/topic/166878-need-help-please/#findComment-880531
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.