PHP Quiz Question Calculations Radios and Checkboxes

Viewed 232

I have a html quiz, the answers are in radio button and checkbox format.

Users must answer all parts of the question correctly in order to score full marks, no split points are awarded.

I have 25 questions in my quiz.

A simplified version of the html quiz is below (showing just 4 questions);

// q1 answer is value 1
<input type="radio" name="form[1-1]" value="1">
<input type="radio" name="form[1-1]" value="2">

// q2 answer is value 3
<input type="radio" name="form[1-2]" value="1">
<input type="radio" name="form[1-2]" value="2">
<input type="radio" name="form[1-2]" value="3">

// q3 answer is value 3
<input type="radio" name="form[1-3]" value="1">
<input type="radio" name="form[1-3]" value="2">
<input type="radio" name="form[1-3]" value="3">

// q4 answer is value 1 AND 2 (both correct answers need to be selected)
<input type="checkbox" name="form[1-4][]" value="1">
<input type="checkbox" name="form[1-4][]" value="2">
<input type="checkbox" name="form[1-4][]" value="3">
<input type="checkbox" name="form[1-4][]" value="4">
// etc

I need to compare the values submitted in the quiz against an array of pre-defined correct answers.

The PHP code I have to check the answers is below;

$total = '0';

// if it is a multiple answer question, then the answer is an array of the correct values
// 'question number' => solution
$solutions = [
  '1-1' => 1, 
  '1-2' => 3, 
  '1-3' => 3, 
  '1-4' => [1,2],
  '1-5' => 3,
  '1-6' => [1,4,6],
  '1-7' => 2,
  '1-8' => [1,2],
  '1-9' => 2,
  '1-10' => 1,
  '1-11' => 4,
  '1-12' => 3,
  '1-13' => [2,4],
  '1-14' => 2,
  '1-15' => 1,
  '1-16' => 1,
  '1-17' => [1,2],
  '1-18' => 2,
  '1-19' => 2,
  '1-20' => 1,
  '1-21' => 3,
  '1-22' => 2,
  '1-23' => 1,
  '1-24' => 3,
  '1-25' => 2
];

// The loop goes through the solutions and compares the answer against the expected solution. 
// If the answer is not present, the ?? null sets it,
foreach ( $solutions as $question => $solution ) {
    $userAnswer = $_POST['form'][$question] ?? null;
    if ( is_array($solution) ){
        $correct = array_intersect($solution, $userAnswer);
        $total += (count($solution) == count($correct));
    }
    else    {
        $total += ($userAnswer == $solution);
    }
}

$marksPerAnswer = 5;
$total = $total * $marksPerAnswer;
$_POST['form']['total'] = $total;
var_dump($_POST);

The results of var_dump($_POST) are;

array (
  'form' => 
  array (
    '1-1' => '1',
    '1-2' => '3',
    '1-3' => '3',
    '1-4' => 
    array (
      0 => '1',
      1 => '2',
    ),
    '1-5' => 
    array (
      0 => '3',
    ),
    '1-6' => 
    array (
      0 => '1',
      1 => '4',
      2 => '6',
    ),
    '1-7' => 
    array (
      0 => '2',
    ),
    '1-8' => 
    array (
      0 => '1',
      1 => '2',
    ),
    '1-9' => 
    array (
      0 => '2',
    ),
    '1-10' => 
    array (
      0 => '1',
    ),
    '1-11' => 
    array (
      0 => '1',
    ),
    '1-12' => 
    array (
      0 => '3',
    ),
    '1-13' => 
    array (
      0 => '1',
      1 => '3',
    ),
    '1-14' => 
    array (
      0 => '2',
    ),
    '1-15' => 
    array (
      0 => '1',
    ),
    '1-16' => 
    array (
      0 => '1',
    ),
    '1-17' => 
    array (
      0 => '2',
      1 => '3',
    ),
    '1-18' => 
    array (
      0 => '2',
    ),
    '1-19' => 
    array (
      0 => '2',
    ),
    '1-20' => 
    array (
      0 => '2',
    ),
    '1-21' => 
    array (
      0 => '2',
    ),
    '1-22' => 
    array (
      0 => '3',
    ),
    '1-23' => 
    array (
      0 => '3',
    ),
    '1-24' => 
    array (
      0 => '3',
    ),
    '1-25' => 
    array (
      0 => '2',
    ),
    'total' => 30,
  ),
)

I have answered the first ten questions correctly (see my array answers match the array solutions 1-1 to 1-10) however the total is only 30, I should have scored at least 50 for the first ten correct answers?

It looks as though the script isn't counting the multiple answer questions like numbers 4, 6 and 8. I don't know why?

Is my code incorrect, or should I change it in some way in order to achieve what I want?

1 Answers

As it looks as though there are some issues with things being multichoice or not, this solution converts everything into arrays and then checks if the answer is equal to the solution...

$total = 0;

foreach ( $solutions as $question => $solution ) {
    $userAnswer = $_POST['form'][$question] ?? null;
    $solution = is_array($solution) ? $solution : [$solution];
    $userAnswer = is_array($userAnswer) ? $userAnswer : [$userAnswer];
    $total += ($userAnswer == $solution);
}
Related