I have this table:
+----+---------+------+----------+
| ID | ID_USER | NOTE | QUESTION |
+----+---------+------+----------+
| 1 | 12 | 3 | 1 |
| 2 | 31 | 2 | 1 |
| 3 | 12 | 9 | 1 |
| 4 | 31 | 8 | 2 |
| 5 | 12 | 10 | 2 |
| 6 | 31 | 3 | 2 |
| 7 | 14 | 4 | 3 |
+----+---------+------+----------+
How can I extract the values in sql table and expose them in an HTML table as the one below?
+----------+----+----+----+
| QUESTION | 12 | 31 | 14 |
+----------+----+----+----+
| 1 | 6 | 2 | 0 |
| 2 | 2 |5.5 | 0 |
| 3 | 0 | 0 | 3 |
+----------+----+----+----+
The values 6, 5.5, 3 are averages. Work with php-mysqli.
I have started code, but i don't know how to convert row in column. My code generate two columns and rows equal with the number of ID-s. I need to generate two rows and columns equal with the number of ID-s.
<?php
include 'bd_cnx.php';
$sql = "SELECT
id_user,
question,
AVG(note) AS medium_note
FROM notes
GROUP BY id_user, question";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<tr><th>QUESTION</th>
<th>'. $row['id_user'] .'</th></tr>';
echo '<tr><td>'. $row['question'] .'</td><td>' . $row['medium_note'] . '</td></tr>';
}
}
?>
Thank you!
</div>