L
L
Lev Rozanov2020-04-17 12:08:22
PHP
Lev Rozanov, 2020-04-17 12:08:22

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>


Then, I wanted the value from the value checkbox to be exploded and checked in a loop.
Those. I need to check:
- make a request to the database about the number of correct answers to the question with id = 55. Write down somewhere.
- make a query to the database about the current answers with the id of the question and the id of the answer. Record somewhere.
- if the number of answers to the question (id) is equal to the number of correct answers (id), then the answer is correct. Record somewhere.

But. The problem is that I don't understand how to organize the cycle correctly, since answers with different id have the same question id.

Maybe I didn't organize the structure of the database or the output in the right way? Or maybe implement a cycle really?

Please, help! Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Panasin, 2020-04-17
@MetisKot

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++;
}

The first cycle we receive questions, then answers and we check them from a DB. If at least one answer is correct in the database, then add 1 to the correct answers. The result will be the sum of the correct answers.

R
Rsa97, 2020-04-17
@Rsa97

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`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question