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!
Do this:
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
Using print_r need the second argument to true - Aubree_Marks commented on April 19th 20 at 12:53
If the amount of replies instead of the string $is = true, check $a++.
- Aubree_Marks commented on April 19th 20 at 12:59
Have done so:
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
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
- josh33 commented on April 19th 20 at 13:17
If you select all the right and wrong, then the answer is count((
- josh33 commented on April 19th 20 at 13:23
But somehow always finds the answers to the second question is correct((
- josh33 commented on April 19th 20 at 13:29
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
- josh33 commented on April 19th 20 at 13:35
With the code figured out. Is not derived from the name of the variable $answer in the second nested foreach. Renamed and it worked!
- josh33 commented on April 19th 20 at 13:38
Decided that I am so much tensed and asked another question)) was not correctly considered empty answers.
- josh33 commented on April 19th 20 at 13:44