我有一個(gè)使用 html 單選按鈕設(shè)計(jì)的測(cè)驗(yàn),計(jì)算由一些 PHP 處理。請(qǐng)參閱下面的基本 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 和 Checkbox Calculation),但是它在具有多個(gè)答案的問(wèn)題上拆分了要點(diǎn)。$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); }}如何只為完全正確的答案分配一分?
1 回答
四季花海
TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
如果您只想計(jì)算完全正確的答案,那么您可以將預(yù)期答案的數(shù)量與匹配的數(shù)量進(jìn)行比較,因此將第一個(gè) if 部分更改為...
if ( is_array($solution) ){
$correct = array_intersect($solution, $userAnswer);
$total += (count($solution) == count($correct));
}
- 1 回答
- 0 關(guān)注
- 146 瀏覽
添加回答
舉報(bào)
0/150
提交
取消
