I'm working on a multi-question quiz powered by PHP and jQuery. After selecting some answers and clicking the Submit button, a user is forwarded to another page which has this answer key:
$totalCorrect = 0;
$answers = [1 => 'A', 2 => 'Jupiter', 3 => 'C', 4 => 'D', 5 => 'A', 6 => 'C', 7 => 'C', 8 => 'C', 9 => 'B', 10 => ''.$Ch.''];
foreach ($answers as $num => $answer)
{
$question = 'q'.$num;
if (isset($_POST[$question]) && $_POST[$question] === $answer)
{
$totalCorrect++;
}
}
$pct = round( (($totalCorrect/count($answers)) * 100), 0);
echo $totalCorrect.' correct ('.$pct.'%)';
All are multiple-choice answers except for #2 (fill-in-the blank) and the last one, which asks users to check all the checkboxes that are correct. Everything works fine except for the last one.
All the code I posted above is preceded by this script, which is designed to make the checkbox answers a little more manageable:
if (isset($_POST))
{
// Check that the 2 correct answers are set, and the 3 incorrect answers are not set
if (isset($_POST['q10-A'], $_POST['q10-B'], $_POST['q10-C']) &&
!isset($_POST['q10-D']) &&
!isset($_POST['q10-E']) &&
!isset($_POST['q10-F']))
{
$Ch = 'A';
}
else
{
$Ch = 'B';
}
}
As you can see, I tried to make it work by giving the value 'A' to a correct radio checkbox group selection (A, B and C, in this case). But it doesn't work. If I replace ''.$Ch.'' with 'A' it still doesn't work.
$answers = [1 => 'A', 2 => 'Jupiter', 3 => 'C', 4 => 'D', 5 => 'A', 6 => 'C', 7 => 'C', 8 => 'C', 9 => 'B', 10 => ''.$Ch.''];
So I'm confused. Can anyone either show me the correct way to do what I'm trying to do or show me an alternative?
Aucun commentaire:
Enregistrer un commentaire