I'd like to create a query with GROUP BY, however not all rows are shown in the result. Here is an example of the database:
id | color | size | price
010101 | orange| S | 11 |
010102 | orange | M | 11 |
010103 | orange | L | 11 |
010201 | green | S | 11 |
etc.
I want to group the results by color. However, only one size shows up. This is what it looks like:
Casual Text Shirt
Orange
S
Green
S
... etc.
Only S shows up, not M or L.
This is my code:
<? php
if (isset($_POST['01'])){
echo "Casual Text Shirt<br>";
try {
$db=new PDO((all db info here));
$query = $db->prepare("SELECT color, size , price FROM shirts WHERE id LIKE '01%' GROUP BY color");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($result as &$data) {
echo "". $data["color"] . "</div><br>";
echo"". $data["size"] . "<br> ";
echo $data["price"] . " €<br><br>";
}
} catch(PDOException $e) {
die("Error!: " . $e->getMessage());
}
}
?>