0

I am trying to work my head round this, I am using the following code to check the answers to a quiz and output either CORRECT or INCORRECT depending on the result of the comparison, and if the answer field is black (which only comes from the feedback form) a different message is displayed. I cant quite work out how to apply the php count function in this situation though, what im trying to get it to do it count the amount of CORRECT and INCORRECT answers and add the two together, and then I can work out a % score from that.

        <?php
// Make a MySQL Connection
// Construct our join query
$query = "SELECT * FROM itsnb_chronoforms_data_answerquiz a, itsnb_chronoforms_data_createquestions
q WHERE a.quizID='$quizID' AND a.userID='$userID' and q.quizID=a.quizID and
a.questionID = q.questionID ORDER BY a.cf_id ASC" or die("MySQL ERROR: ".mysql_error());
$result = mysql_query($query) or die(mysql_error());

// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
if ($row['correctanswer'] == ''){echo '<tr><td style="color:blue;">Thankyou for your feedback</td></tr>';}
elseif ($row['correctanswer'] == $row['quizselectanswer']){
echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>';}
else {echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>';
}}
?>

Iv found this example and have been trying to work out how to work it in, but cant think how to count the results of an if statement with it ?.

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
$result = count($people);

echo $result;
?>
6
  • remove extra whitspaces in your code Commented Nov 28, 2012 at 11:27
  • @RhoHappy if anything the code needs more whitespace. Commented Nov 28, 2012 at 11:35
  • why do you need to use count(), there are many solutions possible Commented Nov 28, 2012 at 11:35
  • Do NOT use the mysql_* API anymore, the community has deprecated it because it is inefficient and highly insecure! Use mysqli or PDO instead with prepared statements. Commented Nov 28, 2012 at 11:40
  • Your code may be open to sql injection attacks! Commented Nov 28, 2012 at 11:41

4 Answers 4

2

Just increment two counters

$correct_answers = 0;
$incorrect_answers = 0;
while ($row = mysql_fetch_array($result)) {
    if ($row['correctanswer'] == '') {...
    } elseif ($row['correctanswer'] == $row['quizselectanswer']) {
        echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>';
        $correct_answers++;
    } else {
        echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>';
        $incorrect_answers++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Simply increase some counter in each branch of your if/elseif/else construct...

$counters = array( 'blank'=>0, 'correct'=>0, 'incorrect'=>0 );
// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
    if ( ''==$row['correctanswer'] ) {
        echo '<tr><td style="color:blue;">Thankyou for your feedback</td></tr>';
        $counters['blank'] += 1;
    }
    elseif ( $row['correctanswer']==$row['quizselectanswer'] ) {
        echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>';
        $counters['correct'] += 1;
    }
    else {
        echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>';
        $counters['incorrect'] += 1;
    }
}

echo '#correct answers: ', $counters['correct'];

Comments

1

How about creating a variable with a count of correct answers? For each question you loop through, increment the value by one.

<?php
    $correct_answers = 0;
    while($questions) {
        if($answer_is_correct) {
            // Increment the number of correct answers.
            $correct_answers++;
            // Display the message you need to and continue to next question.
        }
        else {
            // Don't increment the number of correct answers, display the message
            // you need to and continue to the next question.
        }
    }
    echo 'You got ' . $correct_answers . ' question(s) right!';

Comments

1
 <?php
// Make a MySQL Connection
// Construct our join query
$query = "SELECT ... ";

$result = mysql_query($query) or die(mysql_error());

$countCorrect = 0;
$countIncorrect = 0;

// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
    if ($row['correctanswer'] == ''){echo '<tr><td style="color:blue;">Thankyou for your feedback</td></tr>';}
    elseif ($row['correctanswer'] == $row['quizselectanswer']){
        $countCorrect++;


        echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>';}
        else {
        $countIncorrect++;
        echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>';
        }



}

echo ((int)($countCorrect/($countIncorrect + $countCorrect) * 100)) . "% answers were correct!" ;

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.