Reputation: 396
I have a quiz where by results are sent using session like this to result.php page
{
$_SESSION['result'] = 'Correct Answer!';
}else{
$_SESSION['result'] = 'Wrong Answer!';
}
on the session.php page i would like a counter which counts all 'correct answer!' and 'wrong answer!' . eventually im also trying to figure the arithmetic to display a percentage of correct and wrong results and overall score based on this counter..
any suggestions?
Upvotes: 0
Views: 139
Reputation: 4915
Counters initialization (may be performed every time before stepping):
if(!isset($_SESSION['correctAnswers'])
$_SESSION['correctAnswers'] = 0;
if(!isset($_SESSION['wrongAnswers'])
$_SESSION['wrongAnswers'] = 0;
Counters stepping:
if(...) {
$_SESSION['result'] = 'Correct Answer!';
$_SESSION['correctAnswers'] += 1;
}else{
$_SESSION['result'] = 'Wrong Answer!';
$_SESSION['wrongAnswers'] += 1;
}
Score:
$correctAnswers = $_SESSION['correctAnswers'];
$totalAnswers = $_SESSION['wrongAnswers'] + $correctAnswers;
if($totalAnswers > 0)
$score = $correctAnswers / ($totalAnswers) * 100;
else
$score = 0;
Upvotes: 2
Reputation: 14149
Isn't this just a simple case of wanting to store the number of questions answered and the number of questions answered correctly?
If so the the percentage of correct answers is ($correctCount / $totalCount) * 100
Upvotes: 0