Beställ dina resultat efter series_id
, så alla produkter med samma värde kommer att vara tillsammans.
$stmt = $pdo->prepare("SELECT series_id, product_name
FROM yourTable
ORDER BY series_id");
$stmt->execute();
När du visar resultaten, visa Serierubriken och starta en ny när det ändras:
$last_series = null;
echo "<ul>\n";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($row['series_id'] != $last_series) {
if ($last_series) {
echo "</ul></li>\n";
}
echo "<li>" . $row['series_id'] . " Series Product\n";
echo "<ul>\n";
$last_series = $row['series_id'];
}
echo "<li>" . $row['product_name'] . "</li>\n";
}
if ($last_series) {
echo "</li>\n";
}
echo "</ul>\n";