简体   繁体   中英

Using php's count () command to count the result of an if statement

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;
?>

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

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

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!';
 <?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!" ;

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM