Jump to content

PHP Exam Script


Andrew R

Recommended Posts

I was wondering if anybody could help me at all.  It would so much appreciated.

 

The table structure for entry_test is as follows...

 

id | question |  ans1 | ans2 | ans3 | correctans

 

correctans is stored as a,b,c

 

The code is shown below:

 

<?php 

////db connection here 

if ($_POST['useraction'] != 'checkit') {
$answer = array();
$question = array();
$qnum = array();
$orgid = array();
$i = 0;
$query = "SELECT * from entry_test ORDER by RAND() LIMIT 20";
$result = mysql_query($query, $db);

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$answer[$i] = $row[5];
$question[$i] = $row['1'];
$qnum[$i] = $i;
$orgid[$i] = $row['id'];


$ans1[$i] = $row[2];
$ans2[$i] = $row[3];
$ans3[$i] = $row[4];
$i++;
}
$user_answer = array();
$i = 0;
$j = 1;
print "<FORM ACTION='$PHP_SELF' METHOD='post'><BR>\n";
while ($j <= 20) {
print "Question #" . $j . ': ' . "<b>".$question[$i]."</b><BR>\n";
print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]' VALUE='a'>".$ans1[$i]."<BR>\n";
print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]' VALUE='b'>".$ans2[$i]."<BR>\n";
print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]' VALUE='c'>".$ans3[$i]."<P><P>\n";
$i++;
$j++;
}

//print "<INPUT TYPE='hidden' NAME='qkey' VALUE='$questionkey'>\n"; 
$ii=0;
while ($ii<=19) {
print "<input type='hidden' name='answer[$ii]' value='$answer[$ii]'>\n";
print "<input type='hidden' name='orgid[$ii]' value='$orgid[$ii]'>\n";
$ii++;
} 
print "<INPUT TYPE='hidden' NAME='useraction' VALUE='checkit'>\n"; 
print "<INPUT TYPE='submit' NAME='submitted' VALUE='Check Answers'>\n";
print "</FORM><br>\n";

} else { // <!-- if submitted, check the answers on the form -->

$i=0;
$correct = 0;
$incorrect = 0;
foreach ($_POST['orgid'] as $k =>$v) {
$orgid[$k] = $v;
}

$k = 0;
$m = 0;
while ($k <= 19) {
if ($_POST['user_answer'][$k] == $answer[$k]) {
$correct++;
unset ($_POST['user_answer'][$k]);
unset ($orgid[$k]);
} else {
$incorrect++;
}
$k++;
}
echo "<br><br>";
print "You answered $correct questions correctly and $incorrect questions incorrectly.<br>";
if($correct > 16) { 
echo "<br><br>Well done! You passed with flying colors. I hope you went outside today and got some fresh air and exercise.<br><br>";
} elseif ($correct > 10) {
echo "<br><br>You fell a bit short of a passing grade this time. We're sure you'll do better after you brush up
on your molecular biophysics.<br><br>";
} else {
echo "<br><br>Well you made a mess of that. Try reading a book instead of skateboarding tomorrow.<br><br>";
} 




} // original if

?>

 

Everytime the form is submitted it returns saying all 20 twenty questions are correct.  When you select the correct answer is it marrked incorrect. 

 

Link to comment
https://forums.phpfreaks.com/topic/184918-php-exam-script/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.