根据类别循环结果,但只显示一次类别 [英] Loop results based on category but only show category once
本文介绍了根据类别循环结果,但只显示一次类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含2列的表:
I have a table that has 2 columns:
Product
Title
我正在做
SELECT * FROM products ORDER BY product
我想循环显示Title的所有结果,但每个分组只显示一次Product:
I want to loop all the results of Title but only show Product once per grouping:
Product
--Title
--Title
--Title
Product
--Title
--Title
--Title
我正在做一个常规的while循环,当然,它显示了每一行的Product列.我知道我以前做过此事,但无法记住我的一生!有任何想法吗?谢谢.
I'm doing a regular do while loop and of course it's showing the Product column for each row. I know I've done this before but can't for the life of me remember! Any ideas? Thanks.
推荐答案
由于您已经按产品订购了结果,因此可以执行以下操作:
Since you're already ordering your result by product, you could do something like this:
$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
循环,则相同.
If you want to use your existing do while
-loop instead, it's the same.
这篇关于根据类别循环结果,但只显示一次类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文