0

我正在努力解决这个问题,我正在使用以下代码检查测验的答案,并根据比较结果输出正确或错误,以及答案字段是否为黑色(仅来自反馈表)显示不同的消息。我不能完全弄清楚如何在这种情况下应用 php count 函数,我试图让它来计算正确和错误答案的数量并将两者相加,然后我可以从那。

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

4 回答 4

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++;
    }
}
于 2012-11-28T11:31:01.393 回答
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'];
于 2012-11-28T11:29:37.847 回答
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!';
于 2012-11-28T11:33:01.963 回答
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!" ;

?>
于 2012-11-28T11:34:04.610 回答