测验系统得分php

时间:2015-10-30 11:32:14

标签: php

我正在进行测验页面,我需要一些帮助来计算分数。

  

1正确答案= 1分

     

3错误的答案= -1标记

     

未答复的答案= 0标记

我有代码将所有答案统计为正确,错误或无人接听。

$response = mysql_query("select qus_id, qus_cans from question where qus_id IN ($order) ORDER BY FIELD(qus_id,$order)", $kpsctuts->connect) or die(mysql_error());   
while($result=mysql_fetch_array($response)){
    if($result['qus_cans']==$_POST[$result['qus_id']])
    {
        $right_answer++;
    }
    else if($_POST[$result['qus_id']]==5)
    {
        $unanswered++;
    }
    else
    {
        $wrong_answer++;
    }
    // Enter Code Here
}

输出标记:

<p>Total no. of right answers : <span class="answer"><?php echo $right_answer;?></span></p>
<p>Total no. of wrong answers : <span class="answer"><?php echo $wrong_answer;?></span></p>
<p>Total no. of Unanswered Questions : <span class="answer"><?php echo $unanswered;?></span></p>            

2 个答案:

答案 0 :(得分:1)

首先,您应该将变量$marks定义为0.然后,如果$right_answer有任何值,请将其直接存储在$marks中,因为您必须为正确答案给出+1分。然后同样检查是否存在任何错误答案。

您的代码继续使用以下代码:

$marks = 0;
if($right_answer) $marks = $right_answer;
if($wrong_answer) $marks = $marks - $wrong_answer/3;
$score = number_format((float)$marks,2,'.',''); //Only two decimal places
echo "The score is: ".$score;

答案 1 :(得分:0)

使用bcadd()number_format()

<?php 
     $total = $right_answer - ($wrong_answer/3);
     echo bcadd($total,0,2);
?>