donglianglu8136 2012-11-28 11:24
浏览 99
已采纳

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

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;
?>
  • 写回答

4条回答 默认 最新

  • douduidui1046 2012-11-28 11:29
    关注

    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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)