topflight Posted July 21, 2009 Share Posted July 21, 2009 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";?> Quote Link to comment https://forums.phpfreaks.com/topic/166833-not-getting-score-the-correct-way/ Share on other sites More sharing options...
ignace Posted July 21, 2009 Share Posted July 21, 2009 $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. Quote Link to comment https://forums.phpfreaks.com/topic/166833-not-getting-score-the-correct-way/#findComment-879722 Share on other sites More sharing options...
topflight Posted July 21, 2009 Author Share Posted July 21, 2009 $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? Quote Link to comment https://forums.phpfreaks.com/topic/166833-not-getting-score-the-correct-way/#findComment-879727 Share on other sites More sharing options...
ignace Posted July 21, 2009 Share Posted July 21, 2009 $elr[id] to answers[$elr[id]] access using: $_POST['answers'][..] = (a|b|c|..) Quote Link to comment https://forums.phpfreaks.com/topic/166833-not-getting-score-the-correct-way/#findComment-879740 Share on other sites More sharing options...
topflight Posted July 21, 2009 Author Share Posted July 21, 2009 confused lol please give an example. Quote Link to comment https://forums.phpfreaks.com/topic/166833-not-getting-score-the-correct-way/#findComment-879745 Share on other sites More sharing options...
topflight Posted July 21, 2009 Author Share Posted July 21, 2009 any other help please Quote Link to comment https://forums.phpfreaks.com/topic/166833-not-getting-score-the-correct-way/#findComment-879769 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.