How to check the answers to the questions, if there are multiple answers?

Good day!

There is a table with the following structure:
id-----name----------id_question-----correct_answer
1-----the Answer 1----------55---------------0
2-----the Answer 2----------55---------------1
3-----the Answer 3----------55---------------1
4-----the Answer 4----------55---------------0
5-----the Answer 1----------56---------------0
6-----the Answer 2----------56---------------1
7-----the Answer 3----------56---------------1

Where id - increment, name - test answer id_question - id question, correct_answer - the correct answer (0 - no, 1 - Yes).

Questions and answers now appear as follows
<div class="block">
 <p>Test question 1</p>
 <div class="row_answers">
 <input type="checkbox" name="answer[]" value="id_question-id_answer">
 <p>Answer 1</p>
</div>

 <div class="row_answers">
 <input type="checkbox" name="answer[]" value="id_question-id_answer">
 <p>Answer 2</p>
</div>

 <div class="row_answers">
 <input type="checkbox" name="answer[]" value="id_question-id_answer">
 <p>Answer 3</p>
</div>

 <div class="row_answers">
 <input type="checkbox" name="answer[]" value="id_question-id_answer">
 <p>Answer 4</p>
</div>
</div>
<div class="block">
 <p>Test question 2</p>
 <div class="row_answers">
 <input type="checkbox" name="answer[]" value="id_question-id_answer">
 <p>Answer 1</p>
</div>

 <div class="row_answers">
 <input type="checkbox" name="answer[]" value="id_question-id_answer">
 <p>Answer 2</p>
</div>

 <div class="row_answers">
 <input type="checkbox" name="answer[]" value="id_question-id_answer">
 <p>Answer 3</p>
</div>
</div>


Then, I wanted the value from the value of the checkbox, using explode to split and check in a loop.
I.e. I need to check:
to make a request to the database about the number of correct answers for question with id = 55. Somewhere to record.
to make a request to the database about the current answers with the id of question and id answer. Somewhere to record.
- if the number of answers to the question (id) is equal to the number of correct answers (id), then the answer is correct. Somewhere to record.

But. The problem is that I do not understand how to loop because of responses with different IDs have the same id issues.

Maybe I'm not as organized the structure of the database or output? Or maybe implement a loop really?

Please help! Thanks in advance!
April 19th 20 at 12:30
3 answers
April 19th 20 at 12:32
Solution
Change form on
<input type="checkbox" name="answer[id_question][]" value="id_answer">

$bd = []; //query dB to get all the right answers
$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++;
}

The first cycle get questions then answers and check them with the database. If the database has at least one answer is correct then add 1 to the correct answers. In the end, will be the sum of correct answers.
@Aubree_Marks Thanks for the reply!
Do this:
$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 'Correct answers: '.$a.'<br>';


But it displays: Correct answers: 1.
Regardless of the number of answers.

If you do not choose answers, displays the Correct answers: 0

Is to not choose all the correct answers, for example 2 out of 4 correct, then displays the Correct answers: 0 - josh33 commented on April 19th 20 at 12:35
@josh33, Now test and find the bug. - Aubree_Marks commented on April 19th 20 at 12:38
Don't they have placed the $is. Checked each answer and have the question. Changed the answer.
$answer = $_POST['answer'];

$bd = $pdo->query('SELECT * FROM answers WHERE correct_answer = 1'); 
$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++;
}
echo 'Correct answers: '.$a.'<br>';
- Aubree_Marks commented on April 19th 20 at 12:41
@Aubree_Marks, replaced the code on this, but nothing has changed. Also, for multiple correct answers in $a outputs - 1 - josh33 commented on April 19th 20 at 12:44
The code which I tested. Check that your arrays were arrays.
if ($test) {
 $answer = [
 1 => [9,3],
 2 => [4],
];

 $bd = [
[
 'id' => 2,
 'id_question' => 1,
 'correct_answer' => 1,
],
[
 'id' => 3,
 'id_question' => 1,
 'correct_answer' => 1,
],
[
 'id' => 4,
 'id_question' => 2,
 'correct_answer' => 1,
],

];

 $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++;
}
 echo 'Correct answers: '.$a.'<br>';
exit;
}
- Aubree_Marks commented on April 19th 20 at 12:47
@josh33, you'd better check that returns $bd at the stage of processing of responses. - Aubree_Marks commented on April 19th 20 at 12:50
$bd = $pdo->query('SELECT * FROM answers WHERE correct_answer = 1');
var_dump($bd);

Using print_r need the second argument to true - Aubree_Marks commented on April 19th 20 at 12:53
@josh33, In General, that's all right. - Aubree_Marks commented on April 19th 20 at 12:56
This code displays the sum of the questions with at least 1 correct answer? For example, if two correct answer in one question it is 1. I understand from the task.

If the amount of replies instead of the string $is = true, check $a++.
$a = 0;
 foreach ($answer as $id_quest => $answers) {
 foreach ($answers as $id_answer) {
 foreach ($bd as $bd_element) {
 if ($bd_element['id_question'] == $id_quest and $id_answer == $bd_element['id']){
$a++;
 echo 'id_aswer: '.$id_answer.' right<br>';
 } else {
 echo 'id_aswer: '.$id_answer.' - not true<br>';
}
}
}
 }
- Aubree_Marks commented on April 19th 20 at 12:59
@Aubree_Marks, Yes, I think I found it. Returned not an array.
Have done so:
$bd = $pdo->query('SELECT * FROM answers WHERE correct_answer = 1')->fetchAll();


Now you will get array and according to your answers, but if, for example, in question 4 is answer 2 of them and 3 right and select only one of the right or select one correct and the other incorrect, the entire question will be counted as correct, although not selected all the correct answers or not selected just the right(( - josh33 commented on April 19th 20 at 13:02
@Aubree_Marks, I need to count the answer only if you have a specific question (by id) has chosen all the correct answers. In other cases, the answer is incorrect - josh33 commented on April 19th 20 at 13:05
@josh33,
$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++;
 }
- Aubree_Marks commented on April 19th 20 at 13:08
@Aubree_Marks, thanks for help))

But the result still the same((

If the selected at least one correct answer from several, then the answer is counted. And you need to answer the question were counted, only if you have selected all the correct answers. - josh33 commented on April 19th 20 at 13:11
if ($test) {
 $answer = [
 1 => [1,3],
 2 => [4],
];

 $bd = [
[
 'id' => 2,
 'id_question' => 1,
 'correct_answer' => 1,
],
[
 'id' => 3,
 'id_question' => 1,
 'correct_answer' => 1,
],
[
 'id' => 4,
 '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 'Correct answers: '.$a.'<br>';
exit;
}
// Correct answers: 1
- Aubree_Marks commented on April 19th 20 at 13:14
@Aubree_Marks, if you add more answers, it works as I have((
if ($test) {
 $answer = [
 1 => [2,3,4], // selected all the questions
 2 => [7], // selected all the questions
];

 $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 'Correct answers: '.$a.'<br>';
exit;
}
// Correct answers: 2
- josh33 commented on April 19th 20 at 13:17
@josh33,
foreach ($answer as $id_quest => $answers) {
 $is = true;

 foreach ($bd as $bd_element) {
 $is_bd = true;

 if ($bd_element['id_question'] != $id_quest) continue;
 if (array_search($bd_element['id'], $answers) === false) $is = false;
}
 if ($is) $a++;

 }
- Aubree_Marks commented on April 19th 20 at 13:20
@Aubree_Marks, works great! One but))
If you select all the right and wrong, then the answer is count((

if ($test) {
 $answer = [
 1 => [2,3,4,5,6], // id=6 is not in $db
 2 => [7,8,9,10], // id=10 is not in $db
];

 $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 ($bd as $bd_element) {
 $is_bd = true;

 if ($bd_element['id_question'] != $id_quest) continue;
 if (array_search($bd_element['id'], $answers) === false) $is = false;
}
 if ($is) $a++;

}
 echo 'Correct answers: '.$a.'<br>';
exit;
}
// Correct answers: 2
- josh33 commented on April 19th 20 at 13:23
@josh33,
$quests = [];
 foreach ($bd as $bd_element) {
 $quests[$bd_element['id_question']][] = $bd_element['id'];
}

 $a = 0;
 foreach ($quests as $id_quest => $ans) {
 $is = true;
 foreach ($ans as $an) {
 if (array_search($an, $answer[$id_quest]) === false) $is = false;
}
 foreach ($answer[$id_quest] as $answer) {
 if (array_search($answer, $ans) === false) $is = false;
}
 if ($is) $a++;
 }
- Aubree_Marks commented on April 19th 20 at 13:26
@Aubree_Marks, thank You again!
But somehow always finds the answers to the second question is correct((

if ($test) {
 $answer = [
 1 => [2,3,4,5,12], // considers correct, i.e. takes into account the wrong answers
 2 => [7,8,9,15,20], // with any value in the array counts as a correct answer. Ie always shows the Correct answers: 1
];

 $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,
],

];




$quests = [];
 foreach ($bd as $bd_element) {
 $quests[$bd_element['id_question']][] = $bd_element['id'];
}

 $a = 0;
 foreach ($quests as $id_quest => $ans) {
 $is = true;
 foreach ($ans as $an) {
 if (array_search($an, $answer[$id_quest]) === false) $is = false;
}
 foreach ($answer[$id_quest] as $answer) {
 if (array_search($answer, $ans) === false) $is = false;
}
 if ($is) $a++;
}

 echo 'Correct answers: '.$a.'<br>';
exit;
}
- josh33 commented on April 19th 20 at 13:29
@josh33,
$quests = [];
 foreach ($bd as $bd_element) {
 $quests[$bd_element['id_question']][] = $bd_element['id'];
}

 $a = 0;
 foreach ($quests as $id_quest => $ans) {
 $is = true;
 foreach ($ans as $an) {
 if (array_search($an, $answer[$id_quest]) === false) $is = false;
}
 if ($is) $a++;
 }
- Aubree_Marks commented on April 19th 20 at 13:32
@Aubree_Marks, this option considers positive all the answers.
I just dismantled Your previous option, and in the output I see that the cycle of responses to the first question, the rest flows.

And what's strange: if you replace the id of the first question, for example from 1 to 5, the loop passes through the answers to the second question with id 2

if ($test) {
 $answer = [
 1 => [2,3,4,5,20],
 2 => [7,8,9,24],
 3 => [10,11,12],
];

 $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,
],


[
 'id' => 10,
 'id_question' => 3,
 'correct_answer' => 1,
],
[
 'id' => 11,
 'id_question' => 3,
 'correct_answer' => 1,
],
[
 'id' => 12,
 'id_question' => 3,
 'correct_answer' => 1,
],

];


 $quests = [];
 foreach ($bd as $bd_element) {
 $quests[$bd_element['id_question']][] = $bd_element['id'];
}

 $a = 0;
 foreach ($quests as $id_quest => $ans) {
 $is = true;
 echo '<br><br>Question: No.'.$id_quest.'<br>';

 foreach ($ans as $an) {
 if (array_search($an, $answer[$id_quest]) === false) $is = false;
 echo 'Correct answers: '.$an.' - '.$is.'<br>';
}

 foreach ($answer[$id_quest] as $answer) {
 if (array_search($answer, $ans) === false) $is = false;
 echo 'answer: '.$answer.' - '.$is.'<br>';
}
 if ($is) $a++;
}

 //echo 'Correct answers: '.$a.'<br>';
exit;
}
- josh33 commented on April 19th 20 at 13:35
Thank You very much for the help! Maybe I'll be able to repay You financially?

With the code figured out. Is not derived from the name of the variable $answer in the second nested foreach. Renamed and it worked!

$quests = [];
 foreach ($bd as $bd_element) {
 $quests[$bd_element['id_question']][] = $bd_element['id'];
}

 $a = 0;
 foreach ($quests as $id_quest => $ans) {
 $is = true;
 echo '<br><br>Question: No.'.$id_quest.'<br>';

 foreach ($ans as $an) {
 if (array_search($an, $answer[$id_quest]) === false) $is = false;
 echo 'Correct answers: '.$an.' - '.$is.'<br>';
}

 foreach ($answer[$id_quest] as $answer_NEW) {
 if (array_search($answer_NEW, $ans) === false) $is = false;
 echo 'answer: '.$answer_NEW.' - '.$is.'<br>';
}
 if ($is) $a++;
 }
- josh33 commented on April 19th 20 at 13:38
@josh33, You could remove the second loop as I sent above. You probably missed. - Aubree_Marks commented on April 19th 20 at 13:41
@Aubree_Marks, Yes, I saw this code. But it outputs all the answers.
Decided that I am so much tensed and asked another question)) was not correctly considered empty answers.

$answer = [
 1 => [2,3,4,5,52],
 2 => [7,8,9,52],
 3 => [10,11,12,25],
];

 $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,
],


[
 'id' => 10,
 'id_question' => 3,
 'correct_answer' => 1,
],
[
 'id' => 11,
 'id_question' => 3,
 'correct_answer' => 1,
],
[
 'id' => 12,
 'id_question' => 3,
 'correct_answer' => 1,
],

[
 'id' => 13,
 'id_question' => 4,
 'correct_answer' => 1,
],
[
 'id' => 14,
 'id_question' => 4,
 'correct_answer' => 1,
],
[
 'id' => 15,
 'id_question' => 4,
 'correct_answer' => 1,
],
];


 $quests = [];
 foreach ($bd as $bd_element) {
 $quests[$bd_element['id_question']][] = $bd_element['id'];
}


 $a = 0;
 foreach ($quests as $id_quest => $ans) {
 $is = true;
 echo '<br><br>Question: No.'.$id_quest.'<br>';

 foreach ($ans as $an) {
 if (array_search($an, $answer[$id_quest]) === false) $is = false;
 echo 'Correct answers: '.$an.'<br>';
}

 foreach ($answer[$id_quest] as $answer_give) {
 if (array_search($answer_give, $ans) === false) $is = false;
 echo 'answer: '.$answer_give.'<br>';
}

 if ($is) $a++;
}

 echo '<br><br>Correct answers: '.$a.'<br>';
- josh33 commented on April 19th 20 at 13:44
April 19th 20 at 12:34
SELECT `id_question`,
 COUNT(*) AS `correct_answers_total`,
 SUM(`id` IN (1, 2, 6, 7)) AS `correct_answers_users`
 FROM `answers`
 WHERE `correct_answer` = 1
 GROUP BY `id_question`
April 19th 20 at 12:36
Here I do not understand
to make a request to the database about the current answers with the id of question and id answer. Somewhere to record.

And so to all the data in one request ... why you need a loop to output the results?
select id_question, sum( correct_answer) cnt_correct, count(correct_answer) cnt_total
from table
where id_question = 55 /*if necessary*/
id_question group by

Find more questions by tags MySQLPHP