-1

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>';
    }

?>
Henry
  • 5,195
  • 7
  • 21
  • 34
  • 1
    Just set a variable = 0 at the beginning, increment it at each right answer, then echo it out – General_Twyckenham Jul 31 '14 at 17:27
  • No prob. Check out my updated answer for a much better way to handle the question-answer model. – General_Twyckenham Jul 31 '14 at 18:01
  • For the extra question, you'd have to store the question and map (connect) it to the right answer number. For example, if you created another array (`$questions`) and set `$question[$i]="Your question here"`, then when you print out, you'd add `"Question $i - $questions[$i]..."`, or something like that. – General_Twyckenham Jul 31 '14 at 19:15
  • Thanks, i think i made a hash of it...can you see what I am missing? [thanks a ton BTW] - I made changes above – Henry Jul 31 '14 at 20:27
  • Looks like you're just missing printing the correct answer for an incorrect question: `! The correct answer was $answer.` – General_Twyckenham Jul 31 '14 at 20:48

2 Answers2

1

Here is the trivial way to do it, but I also included a much better way to do it:

// change the quest1 to the right answer
$score = 0;  //Initialize the variable used to keep score.

if($quest1 == "3") { 
$score++;    //Increment the score by 1 at each correct answer
$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/>"; 
}
...

echo "You got $score questions correct";  //Print out the score

A better way to do this would be to have the questions in an array, so that instead of hardcoding each answer, you can just loop through them all:

$answers = array(); //Initialize array
    $answers[1] = "1";  //Set the answers
    $answers[2] = "3";
    $answers[3] = "1";

    $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
        $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 is <span class='green'>correct</span>, well done!<br/>";
            $score++;  
        }
        else {
            $test_complete .="Question $i is <span class='red'>incorrect</span>! The correct answer was $answer.<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>';
    }
General_Twyckenham
  • 2,161
  • 2
  • 21
  • 36
0

First attempt

<?php
$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'];

$score = 0;

// 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") { 
$score++;
$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") { 
$score++;
$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") { 
$score++;
$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") { 
$score++;
$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") { 
$score++;
$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>'; 
}}}
echo "<p>Your score is: $score</p>";
?>

Short opening tags are baaaad!

wbrzezin
  • 19
  • 3
  • Thanks - Also - what do you mean by short opening tags are bad? Could you explain please? Thanks – Henry Jul 31 '14 at 17:56
  • Thats, why [short tag are bad](http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use/200666#200666) – wbrzezin Aug 01 '14 at 06:45