【发布时间】:2020-09-21 23:41:44
【问题描述】:
我有一个使用 html 单选按钮设计的测验,计算由一些 PHP 处理。
请参阅下面的基本 html 布局(为了便于阅读,删除了一些元素);
// q1 answer is value 2
<input type="radio" name="form[1-1]" value="1">
<input type="radio" name="form[1-1]" value="2">
// q2 answer is value 1
<input type="radio" name="form[1-2]" value="1">
<input type="radio" name="form[1-2]" value="2">
// q1 answer is value 2
<input type="radio" name="form[1-3]" value="1">
<input type="radio" name="form[1-3]" value="2">
// q4 answer is value 1 AND 2 AND 4
<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">
我目前拥有的 PHP 代码可以运行(感谢 PHP Quiz Radio and Checkbox Calculation),但是它会在具有多个答案的问题上拆分分数。
$solutions = ['1-1' => 2, '1-2' => 1, '1-3' => 2, '1-4' => [1,2,4]];
foreach ( $solutions as $question => $solution ) {
$userAnswer = $_POST['form'][$question] ?? null;
if ( is_array($solution) ){
$marksPerAnswer = 1/count($solution);
$correct = array_intersect($solution, $userAnswer);
$total += $marksPerAnswer * count($correct);
}
else {
$total += ($userAnswer == $solution);
}
}
我怎样才能只为完全正确的答案打一分?
【问题讨论】: