Jump to content

[SOLVED] Not adding correctly


topflight

Recommended Posts

Hello I have created like my own exam system, and I was testing it put and when I put everything correct my score still comes out to 90 instead of 100 please help.

 

exam.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" name="counter">

<input type="hidden" size="8" name="timer">



<script>

<!--

//

var milisec=0

var seconds=30

document.counter.timer.value='30'



function display(){

if (milisec<=0){

    milisec=9

    seconds-=1

}

if (seconds<=-1){

    milisec=0

    seconds+=1

}

else

    milisec-=1

    document.counter.d2.value=seconds+"."+milisec

    setTimeout("display()",100)

}

display()

-->

</script>

<?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("jbusecure/dbconfig09.php");

$a1 = $_POST[1];
$a2 = $_POST[2];
$a3 = $_POST[3];
$a4 = $_POST[4];
$a5 = $_POST[5];
$a6 = $_POST[6];
$a7 = $_POST[7];
$a8 = $_POST[8];
$a9 = $_POST[9];
$a10 = $_POST[10];
$a11 = $_POST[11];
$a12 = $_POST[12];
$a13 = $_POST[13];
$a14 = $_POST[14];
$a15 = $_POST[15];
$a16 = $_POST[16];
$a17 = $_POST[17];
$a18 = $_POST[18];
$a19 = $_POST[19];
$a20 = $_POST[20];

$correct = 0; # Initialize the Variable

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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





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

echo "Your score was $score";

?>

 

 

 

Link to comment
Share on other sites

array(23) { ["timer"]=>  string(0) "" [19]=>  string(1) "a" [13]=>  string(1) "a" [5]=>  string(1) "a" [4]=>  string(1) "a" [10]=>  string(1) "a" [14]=>  string(1) "a" [21]=>  string(1) "a" [12]=>  string(1) "a" [22]=>  string(1) "a" [20]=>  string(1) "a" [18]=>  string(1) "a" [7]=>  string(1) "a" [16]=>  string(1) "a" [8]=>  string(1) "a" [11]=>  string(1) "a" [2]=>  string(1) "a" [15]=>  string(1) "a" [6]=>  string(1) "a" [17]=>  string(1) "a" [3]=>  string(1) "a" ["now"]=>  string(0) "" ["viewexam"]=>  string(3) "yes" } Your score was 90

Link to comment
Share on other sites

In correct_exam.php, you are checking for the value of $_POST[1], but there is no key [1] or [9] in your var_dump:

 

array(23) { 
["timer"]	=>  string(0) "" 
[19]		=>  string(1) "a" 
[13]		=>  string(1) "a" 
[5]			=>  string(1) "a" 
[4]			=>  string(1) "a" 
[10]		=>  string(1) "a" 
[14]		=>  string(1) "a" 
[21]		=>  string(1) "a" 
[12]		=>  string(1) "a" 
[22]		=>  string(1) "a" 
[20]		=>  string(1) "a" 
[18]		=>  string(1) "a" 
[7]			=>  string(1) "a" 
[16]		=>  string(1) "a" 
[8]			=>  string(1) "a" 
[11]		=>  string(1) "a" 
[2]			=>  string(1) "a" 
[15]		=>  string(1) "a" 
[6]			=>  string(1) "a" 
[17]		=>  string(1) "a" 
[3]			=>  string(1) "a" 
["now"]		=>  string(0) "" 
["viewexam"]=>  string(3) "yes" 
}
Your score was 90

 

You also have a key [21] and [22]. Did you notice?

Link to comment
Share on other sites

ok I change the ID number in the database and now I am getting the following....

 

 

array(23) { ["timer"]=>  string(0) "" [17]=>  string(1) "a" [10]=>  string(1) "a" [5]=>  string(1) "a" [9]=>  string(1) "a" [19]=>  string(1) "a" [20]=>  string(1) "a" [15]=>  string(1) "a" [6]=>  string(1) "a" [13]=>  string(1) "a" [21]=>  string(1) "a" [1]=>  string(1) "a" [4]=>  string(1) "a" [7]=>  string(1) "a" [18]=>  string(1) "a" [3]=>  string(1) "a" [12]=>  string(1) "a" [11]=>  string(1) "a" [16]=>  string(1) "a" [2]=>  string(1) "a" [14]=>  string(1) "a" ["now"]=>  string(0) "" ["viewexam"]=>  string(3) "yes" } Your score was 95

 

Their is 21 questions in the database, but I have it get a random 20. so do you think that can might be the problem.

Link to comment
Share on other sites

Yes, this is probably where your problem comes from.

 

I just want to add, that this exam system would be fundamentally flawed, because if the user simply views the source code, they would have all of the correct answers. If it matters, it should be changed.

 

When I have created an exam in the past, the questions, answers, and scores were held in an XML file, and then parsed with SimpleXML for question and answers retrieval, and also for scoring. This makes changing the questions, answers, and scoring easy too. An instructor can just create a new XML file, and the new test is ready to go.

Link to comment
Share on other sites

Well the user cant find the correct answer becuase I have it set up like the following:

 

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

 

then it adds one point, I have it like that for every question but my question is that how come when I made all the right answer to be "A" and then I select A on all the answers my result comes out ta 95?

 

 

Link to comment
Share on other sites

OK....

 

I just don't know. It's hard to diagnose because there are quite a few things that look wrong. I think you should run your query directly inside MySQL or phpMyAdmin, and see if you are getting what you expect. Also, until you validate the output of your exam, your form itself could be messing up what you think is perfect. Maybe somebody else will chime in.

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.