0

我有一个测验,使用这样的会话将结果发送到 result.php 页面

    {
        $_SESSION['result'] = 'Correct Answer!';
    }else{
        $_SESSION['result'] = 'Wrong Answer!';
    }

在 session.php 页面上,我想要一个计数器来计算所有“正确答案!” 和“错误的答案!” . 最终我还试图计算算术以显示正确和错误结果的百分比以及基于此计数器的总分..

有什么建议么?

4

2 回答 2

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;
于 2012-04-28T21:05:58.297 回答
0

这不只是想要存储回答的问题数和正确回答的问题数的简单案例吗?

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

于 2012-04-28T21:04:30.840 回答