Companions of StackOverFlow, I have the following doubt, I am trying to make a simple test or evaluation system in php and I created my database in the following way:
+----------------+
| Tables_in_game |
+----------------+
| answers |
| questions |
+----------------+
Questions table
+----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| question | text | NO | | NULL | |
| type | varchar(5) | NO | | NULL | |
+----------+------------+------+-----+---------+----------------+
Answer table (Answers)
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_question | int(11) | NO | | NULL | |
| answer | text | NO | | NULL | |
| is_right | tinyint(1) | NO | | NULL | |
+-------------+------------+------+-----+---------+----------------+
My PHP PDO query is as follows:
$db = new PDO('mysql:host=localhost;dbname=game', 'root', '');
$stmt = $db->prepare('SELECT * FROM questions LEFT JOIN answers ON (questions.id = answers.id_question) WHERE questions.type = :type');
$type = "quiz3";
$stmt->bindParam(':type', $type);
$stmt->execute();
The problem is that when going through it, it prints the question several times according to the answers that the question has:
<div class="container">
<?php foreach ($stmt as $val): ?>
<p>
<?= $val['question']; ?>
</p>
<ul>
<li>
<?= $val['answer']; ?>
</li>
</ul>
<?php endforeach ?>
</div>
What I need is to be able to print the question only once and to be able to print all the possible answers.
As an example I show how it should look:
This is question #1............
- Answer 1
- Answer #2
- Answer #3
- Answer #4
You can make a query by combining
GROUP_CONCAT
andGROUP_BY
, that way it will bring you a column with all the answers and a separator. Then you explode that column.For example:
You would have a result something like this:
Reading in PHP
The query written like this will bring you an array similar to this:
This would be an example of code to read the results:
This code will print this:
More data in the
GROUP_CONCAT
and internal orderYou can add more data in the
GROUP_CONCAT
, and we can also order them by any column if we want. We will also applyCOALESCE
, to make sure that we always have the same data, since if there is any valueNULL
in any column our beautiful code will fail.Let's see:
Now the column
all_answers
will come like this:Better let's see an example of the full array:
Now we have the answer with its
id
next to it and a period. This is a handy way to bring in grouped data without having to repeat rows.If we read the results with the same code as above, we would have something like this:
USE IN YOUR sql QUERY A "list group" this is used to group according to the data that you do not want to be repeated
example "SELECT * FROM table name list group table field you don't want to repeat"