我有一個使用 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 和 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); }}如何只為完全正確的答案分配一分?
1 回答

四季花海
TA貢獻1811條經驗 獲得超5個贊
如果您只想計算完全正確的答案,那么您可以將預期答案的數量與匹配的數量進行比較,因此將第一個 if 部分更改為...
if ( is_array($solution) ){
$correct = array_intersect($solution, $userAnswer);
$total += (count($solution) == count($correct));
}
- 1 回答
- 0 關注
- 127 瀏覽
添加回答
舉報
0/150
提交
取消