我有一个mysql表。像这样的专栏
series_id, series_color, product_name
我应该series_id product
像小组一样列出。
我想要像这样的列表,所有相同的saries_id像这样回显我的屏幕
A12 Series Product
- Milk
- Tea
- sugar
- water
B12 Series Product
- Water
- Banana
- Cofee
- Tea
按排序结果series_id
,因此所有具有相同值的产品将在一起。
$stmt = $pdo->prepare("SELECT series_id, product_name
FROM yourTable
ORDER BY series_id");
$stmt->execute();
然后在显示结果时,显示Series标头,并<ul>
在更改时开始新的标头:
$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";