How do I create a test in PHP?
There is a test in php.
I do this: by default, the php handler has two variables - $ot and $not-correct and incorrect answers, initially I equated them to 0, for the correct answer I raise $ok by 1, for the wrong one-I raise $not by one.
code example:
<?php
$ot = 0;
$not = 0;
if ($_POST[q1] == a){$ot++;} else {$not++;}
if ($_POST[q2] == a){$ot++;} else {$not++;}
?>
In questions with radio buttons, there are no problems, only one option. But in questions with checkbox, I can't check the correctness of the selected answers.
Tell me how to implement this in the framework my example.
Here is the html code
<div>
<p>1. ВОПРОС 1...</p>
<input name="q1[]" type="checkbox" value="a"> 1</label><br>
<input name="q1[]" type="checkbox" value="b"> 2</label><br>
<input name="q1[]" type="checkbox" value="c"> 3</label><br>
</div>
And here is a part of my php code
<?php
$ot = 0;
$not = 0;
$q1 = POST_['q1'];
foreach($q1 as $value) {
....
}
?>
Here's what to put instead ... I can't understand. the correct answer is 1 and 3 checkboxes, for example.
0
1 answers
Basic outline of the solution. Can be improved.
$successCount = 0;
$errorsCount = 0;
$validAnswers = [
'q1' => [1, 3],
'q2' => ['London'],
];
foreach($_POST as $questionKey => $userAnswer) {
// проверяем, что такой вопрос действительно есть в списке
if (!empty($validAnswers[$questionKey])) {
// если не чекбоксы, а "радиобаттон", т.е. только 1 правильный вариант - всё-равно приводим к массиву
$userAnswer = !is_array($userAnswer) ? [$userAnswer] : $userAnswer;
$successCount += array_intersect($userAnswer, $rightAnswers);
$errorsCount += array_diff($userAnswer, $rightAnswers)
}
}
1
Author: Lark_z, 2018-02-26 13:55:04