【问题标题】:PHP Quiz Question Calculations Radios and CheckboxesPHP测验问题计算单选和复选框
【发布时间】:2020-06-17 14:50:26
【问题描述】:

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

用户必须正确回答问题的所有部分才能获得满分,不计分。

我的测验有 25 个问题。

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

// 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

我需要将测验中提交的值与一组预定义的正确答案进行比较。

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

$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 分?

脚本似乎没有计算数字 4、6 和 8 等多选题。我不知道为什么?

我的代码不正确,还是我应该以某种方式更改它以实现我想要的?

【问题讨论】:

  • 根据您的 $_POST 数组,1-5 之后的所有内容都作为数组返回(即使是那些只有一个答案的)。因此,它们将被捕获。不知道这样是不是解决办法,但肯定会影响结果?
  • 它将在 html 中。对于您的复选框,因为您要求的答案不止一个,所以您添加了[]。如果 q4 下面的问题也有[],则可以看到此输出。我在很大程度上假设,因为我们看不到第四季度。只有复选框 q 应该有额外的 []...
  • 在你的回答中,一些看起来不应该是多项选择的问题有用户回答的数组。例如 '1-5' =&gt; 3, 的预期和 '1-5' =&gt; array ( 0 =&gt; '3', ), 的用户输入

标签: php html arrays


【解决方案1】:

由于看起来是否存在多选问题,因此此解决方案将所有内容转换为数组,然后检查答案是否等于解决方案...

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

【讨论】:

  • 这是完美的 - 谢谢!你能解释一下我哪里出错了,以便我学习吗?
  • 正如我在评论中所说,有些非多选选项似乎将数组作为答案,这有点奇怪。 '1-5' 例如。但由于您只需要完全匹配,因此可以轻松地将所有内容转换为多项选择,然后比较结果。
猜你喜欢
  • 2020-08-15
  • 2013-11-22
  • 2011-02-08
  • 1970-01-01
  • 2013-11-18
  • 2011-03-15
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多