if ($test) {
$answer = [
1 => [2,3,4], // выбраны не все вопросы
2 => [7], // выбраны не все вопросы
];
$bd = [
[
'id' => 2,
'id_question' => 1,
'correct_answer' => 1,
],
[
'id' => 3,
'id_question' => 1,
'correct_answer' => 1,
],
[
'id' => 4,
'id_question' => 1,
'correct_answer' => 1,
],
[
'id' => 5,
'id_question' => 1,
'correct_answer' => 1,
],
[
'id' => 7,
'id_question' => 2,
'correct_answer' => 1,
],
[
'id' => 8,
'id_question' => 2,
'correct_answer' => 1,
],
[
'id' => 9,
'id_question' => 2,
'correct_answer' => 1,
],
];
$a = 0;
foreach ($answer as $id_quest => $answers) {
$is = true;
foreach ($answers as $id_answer) {
$is_a = false;
foreach ($bd as $bd_element) {
if ($bd_element['id_question'] != $id_quest) continue;
if ($id_answer == $bd_element['id']) {
$is_a = true;
}
}
if (!$is_a) $is = false;
}
if ($is) $a++;
}
echo 'Правильных ответов: '.$a.'<br>';
exit;
}
// Правильных ответов: 2
$bd = $pdo->query('SELECT * FROM answers WHERE correct_answer = 1')->fetchAll();
$answer = $_POST['answer'];
$bd = $pdo->query('SELECT * FROM answers WHERE correct_answer = 1');
$a = 0;
foreach ($answer as $id_quest => $answers) {
foreach ($answers as $id_answer) {
$is = false;
foreach ($bd as $bd_element) {
if ($bd_element['id_question'] == $id_quest and $id_answer == $bd_element['id']) $is = true;
}
if ($is) $a++;
}
}
echo 'Правильных ответов: '.$a.'<br>';
<form action="" method="POST">
<input type="text" name="question[]" value="Вопрос 1">
<input type="text" name="answer[1][]" value="Ответ 1 на 1 вопрос">
<input type="text" name="answer[1][]" value="Ответ 2 на 1 вопрос">
<input type="text" name="answer[1][]" value="Ответ 3 на 1 вопрос">
<input type="text" name="question[]" value="Вопрос 2">
<input type="text" name="answer[2][]" value="Ответ 1 на 2 вопрос">
<input type="text" name="answer[2][]" value="Ответ 2 на 2 вопрос">
<input type="text" name="question[]" value="Вопрос 3">
<input type="text" name="answer[3][]" value="Ответ 1 на 3 вопрос">
<input type="text" name="answer[3][]" value="Ответ 2 на 3 вопрос">
<input type="text" name="answer[3][]" value="Ответ 3 на 3 вопрос">
<input type="text" name="answer[3][]" value="Ответ 4 на 3 вопрос">
<input type="submit" name="test" value="Тест">
</form>
$c = sizeof($question);
for($i =0;$i<$c;++$i){
echo $question[$i].'<br>';
$cc = sizeof($answers[$i]);
for($ii = 0; $ii < $cc; ++$ii){
echo $answers[$i][$ii].'<br>';;
}
}
$c = sizeof($question);
for($i = 0; $i<$c; ++$i){
foreach ($question as $question_name) {
echo $question_name.'<br>';
}
$cc = sizeof($answers[$i]);
for($i = 0; $i < $cc; ++$i){
foreach ($answers as $answer) {
foreach ($answer as $answer_name) {
echo $answer_name.'<br>';
}
}
}
}
Если выбрать все правильные и один или несколько неправильных, то ответ засчитывает((