我有一个有 2 列的表格:
Product
Title
我正在做:
SELECT * FROM products ORDER BY product
我想循环标题的所有结果,但每个分组只显示一次产品:
Product
--Title
--Title
--Title
Product
--Title
--Title
--Title
我正在执行常规 do while 循环,当然它显示每行的“产品”列。我知道我以前做过这样的事,但我一辈子都记不起来!有任何想法吗?谢谢。
最佳答案
由于您已经按产品对结果进行排序,因此您可以执行以下操作:
$currentProduct = null;
foreach($products as $product) {
if ($currentProduct != $product['product']) {
// We got a new product. Show it with some fancy html
// Then store it in $currentProduct for the next iteration
$currentProduct = $product['product'];
}
// Show the title with some fancy html
}
如果您想使用现有的 do while
-loop,效果是一样的。
关于php - 根据类别循环结果,但仅显示类别一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38224987/