PHP测验问题计算单选和复选框 [英] PHP Quiz Question Calculations Radios and Checkboxes

查看:103
本文介绍了PHP测验问题计算单选和复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html测验,答案采用单选按钮和复选框格式。

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.

我的测验中有25个问题。

I have 25 questions in my quiz.

下面是html测验的简化版本(仅显示4个问题);

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.

我要检查答案的PHP代码如下;

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);

var_dump($ _ POST)的结果是;

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,
  ),
)

我正确回答了前十个问题(请参阅我的数组答案与数组解1-1到1-10匹配),但是总数只有30,我应该在前十个正确答案中至少获得50分吗?

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?

脚本似乎没有计算数字4、6和8之类的多个答案问题。我不知道为什么?

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

我的代码是我的代码吗

推荐答案

看起来好像有一些错误,还是应该以某种方式更改它以实现我想要的?某些问题是否为选择题,此解决方案将所有内容转换为数组,然后检查答案是否等于解决方案...

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;

$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);
}

这篇关于PHP测验问题计算单选和复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆