Answer the question
In order to leave comments, you need to log in
How to check answers to questions if there are several answers?
Good day!
There is a table with the following structure:
id-----name----------id_question-----correct_answer
1-----Answer 1----------55-- -------------0
2-----Answer 2----------55------1
3 -----Answer 3----------55---------------1
4-----Answer 4-------- --55---------------0
5-----Answer 1----------56------------ ---0
6-----Answer 2----------56---------------1
7-----Answer 3--- -------56---------------1
Where id - increment, name - test answer, id_question - question id, correct_answer - correct answer (0 - no, 1 - Yes).
Questions and answers are now displayed as follows
<div class="block">
<p>Тест вопроса №1</p>
<div class="row_answers">
<input type="checkbox" name="answer[]" value="id_question-id_answer">
<p>Ответ 1</p>
</div>
<div class="row_answers">
<input type="checkbox" name="answer[]" value="id_question-id_answer">
<p>Ответ 2</p>
</div>
<div class="row_answers">
<input type="checkbox" name="answer[]" value="id_question-id_answer">
<p>Ответ 3</p>
</div>
<div class="row_answers">
<input type="checkbox" name="answer[]" value="id_question-id_answer">
<p>Ответ 4</p>
</div>
</div>
<div class="block">
<p>Тест вопроса №2</p>
<div class="row_answers">
<input type="checkbox" name="answer[]" value="id_question-id_answer">
<p>Ответ 1</p>
</div>
<div class="row_answers">
<input type="checkbox" name="answer[]" value="id_question-id_answer">
<p>Ответ 2</p>
</div>
<div class="row_answers">
<input type="checkbox" name="answer[]" value="id_question-id_answer">
<p>Ответ 3</p>
</div>
</div>
Answer the question
In order to leave comments, you need to log in
Change the shape to
<input type="checkbox" name="answer[id_question][]" value="id_answer">
$bd = []; //запрос в бд для получения всех верных ответов
$a = 0;
foreach ($answer as $id_quest => $answers) {
$is = false;
foreach ($answers as $id_answer) {
foreach ($bd as $bd_element) {
if ($bd_element['id_question'] == $id_quest and $id_answer == $bd_element['id']) $is = true;
}
}
if ($is) $a++;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question