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/166833-not-getting-score-the-correct-way/
Share on other sites

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

 

Doesn't work. Therefor this never gets executed:

 

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

 

$correct doesn't get defined and when you divide it by 3 it has the value 0 (null to integer = 0) divided by 3 = 0 * 100 = 0. $s3 = 0; The reason that you also get 33 is because it somehow passes one if statement whereby $correct = 1 / 3 = 0.333333.. * 100 = round(33.333333..) = 33.

 

Edit: I'm currently figuring out which if statement can pass and how.

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

 

Doesn't work. Therefor this never gets executed:

 

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

 

$correct doesn't get defined and when you divide it by 3 it has the value 0 (null to integer = 0) divided by 3 = 0 * 100 = 0. $s3 = 0; The reason that you also get 33 is because it somehow passes one if statement whereby $correct = 1 / 3 = 0.3333333 * 100 = round(33.33) = 33

 

so how can i fix it?

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.