使用php的count()命令来计算if语句的结果

时间:2012-11-28 11:24:33

标签: php mysql if-statement count

我正在努力解决这个问题,我正在使用以下代码来检查测验的答案并输出CORRECT或INCORRECT,具体取决于比较的结果,如果答案字段为黑色(仅限于来自反馈表格)显示不同的消息。 我不知道如何在这种情况下应用如何应用php计数功能,我试图让它做它计算CORRECT和INCORRECT答案的数量并将两者加在一起,然后我可以计算出%得分这一点。

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

我发现这个例子并且一直试图弄清楚如何使用它,但是不能想到如何用它来计算if语句的结果?

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

echo $result;
?>

4 个答案:

答案 0 :(得分:2)

只需递增两个计数器

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

答案 1 :(得分:1)

只需在if / elseif / else构造的每个分支中增加一些计数器......

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

答案 2 :(得分:1)

如何创建具有正确答案数的变量?对于循环的每个问题,将值递增1。

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

答案 3 :(得分: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!" ;

?>