$ _session价值计数器

时间:2012-04-28 21:01:13

标签: php session counter

我有一个测验,其结果通过这样的会话发送到result.php页面

    {
        $_SESSION['result'] = 'Correct Answer!';
    }else{
        $_SESSION['result'] = 'Wrong Answer!';
    }
在session.php页面上的

我想要一个计数器来计算所有'正确的答案!'和'错误的答案!' 。最终我还试图通过计算算法来显示正确和错误结果的百分比以及基于此计数器的总分数。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

计数器初始化(可在每次步进之前执行):

if(!isset($_SESSION['correctAnswers'])
    $_SESSION['correctAnswers'] = 0;
if(!isset($_SESSION['wrongAnswers'])
    $_SESSION['wrongAnswers'] = 0;

计数器踩:

if(...) {
    $_SESSION['result'] = 'Correct Answer!';
    $_SESSION['correctAnswers'] += 1;
}else{
    $_SESSION['result'] = 'Wrong Answer!';
    $_SESSION['wrongAnswers'] += 1;
}

分:

$correctAnswers = $_SESSION['correctAnswers'];
$totalAnswers = $_SESSION['wrongAnswers'] + $correctAnswers;
if($totalAnswers > 0)
    $score = $correctAnswers / ($totalAnswers) * 100;
else 
    $score = 0;

答案 1 :(得分:0)

这不是一个简单的案例,想要存储已回答的问题数量和正确回答的问题数量吗?

如果是这样,正确答案的百分比为($correctCount / $totalCount) * 100