Companions of StackOverFlow, I have the following question, I am trying to make a simple system of test or evaluation in php and create my database in the following way:
+----------------+
| Tables_in_game |
+----------------+
| answers |
| questions |
+----------------+
Table of questions (Questions)
+----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| question | text | NO | | NULL | |
| type | varchar(5) | NO | | NULL | |
+----------+------------+------+-----+---------+----------------+
Answers table
+-------------+------------+------+-----+---------+----------------+
| 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 the following:
$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 I go through it, I print 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 once and print all the possible answers.
As an example I show how it should be:
This is question # 1 ............
Reply # 1 Reply # 2 Reply # 3 Reply # 4