I have a multiple choice exam script which works great by giving the user their correct/ incorrect answer. The only thing that is missing is their score.
Can anyone suggest how I might go about adding this feature - i.e. an extra script that echoes out the users score.
<?
$page_title = "The Simple Quiz Script";
// If the form is submitted run the script
if(isset($_POST['submit'])){
$quest1 = $_POST['quest1'];
$quest2 = $_POST['quest2'];
$quest3 = $_POST['quest3'];
$quest4 = $_POST['quest4'];
$quest5 = $_POST['quest5'];
// Lets make sure that everything has been submitted
// This might be the section that would need to be totaled to produce a score.
if($quest1 == NULL OR $quest2 == NULL OR $quest3 == NULL OR $quest4 == NULL OR $quest5 == NULL OR $quest6 == NULL OR $quest7 == NULL OR $quest8 == NULL OR $quest9 == NULL OR $quest10 == NULL){
$test_complete .='Please complete the quiz! You need to answer all the questions <a href="javascript:history.go(-1)">Go Back</a>';
}else{
// change the quest1 to the right answer
if($quest1 == "3") {
$test_complete .="Question 'is this a agood script' is <span class='green'>correct</span>, well done!<br/>";
}else{
$test_complete .="Question one 'is this a agood script' is <span class='red'>incorrect</span>!<br/>";
}
// change the quest2 to the right answer
if($quest2 == "2") {
$test_complete .="Question two is <span class='green'>correct</span>, well done!<br/>";
}else{
$test_complete .="Question two is <span class='red'>incorrect</span>!<br/>";
}
// change the quest3 to the right answer
if($quest3 == "1") {
$test_complete .="Question three is <span class='green'>correct</span>, well done!<br/>";
}else{
$test_complete .="Question three is <span class='red'>incorrect</span>!<br/>";
}
// change the quest4 to the right answer
if($quest4 == "3") {
$test_complete .="Question four is <span class='green'>correct</span>, well done!<br/>";
}else{
$test_complete .="Question four is <span class='red'>incorrect</span>!<br/>";
}
// change the quest5 to the right answer
if($quest5 == "2") {
$test_complete .="Question five is <span class='green'>correct</span>, well done!<br/>";
}else{
$test_complete .="Question five is <span class='red'>incorrect</span>!<br/>";
}
// Now lets see if all the questions are correct, this must match the above quest settings
if($quest1 == "3" & $quest2 == "2" & $quest3 == "1" & $quest4 == "3" & $quest5 == "2" & $quest6 == "3" & $quest7 == "2" & $quest8 == "1" & $quest9 == "3" & $quest10 == "2"){
$test_complete .="<p>Congratulations, you got all the questions correct!</p>";
}else{
// If any of the questions are not correct lets tell them
$test_complete .='<p>Your not there just yet! <a href="javascript:history.go(-1)">Try again</a></p>';
}}}
?>
Any pointers here would be very much appreciated - i'm not sure where to start and I am very new to php. Thanks
+++++
Also! Just to add....
This code has been modified - please see below -
This code is trying to also show the question that the user submitted and whether that is wrong or right....
<?
$answers = array(); //Initialize array
$answers[1] = "3"; //Set the answers
$answers[2] = "3";
$answers[3] = "3";
$answers[4] = "3"; //Set the answers
$answers[5] = "3";
$answers[6] = "3";
$answers[7] = "3"; //Set the answers
$answers[8] = "3";
$answers[9] = "3";
$answers[10] = "3";
$questions=array("question 123","question 456","question 789");
$score= 0; //Initialize score
for($i = 1; $i <= sizeof($answers); $i++) { //Start looping through the answers
$answer = $answers[$i]; //Get the correct answer for the current question
$questions = $question[$i] //Get the correct answer for the current question
$index = "quest$i"; //Slightly confusing here. You create a string with the current index, ie. quest1.
//This will allow you to get the submitted answer in the POST request
$submitted_answer = $_POST[$index]; //Get the submitted answer for this question
if($answer === $submitted_answer) { //If the answer is correct, increment score and add "Correct" response
$test_complete .= "Question $i - $questions[$i] <span class='green'>correct</span>, well done!<br/>";
$score++;
}
else {
$test_complete .="Question $i is <span class='red'>incorrect</span>!<br/>";
}
}
if($score === sizeof($answers)) { //All answers are correct
$test_complete .="<p>Congratulations, you got all the questions correct!</p>";
}
else { //Some answers are wrong
$test_complete .='<p>Your not there just yet! <a href="javascript:history.go(-1)">Try again</a></p>';
}
?>