循环结果基于类别但仅显示类别一次

时间:2016-07-06 13:14:18

标签: php mysql

我有一个包含2列的表:

// Create an event generator for each kind of event
EventGenerator eventGenerator1 = new EventGenerator();

EventGenerator eventGenerator2 = new EventGenerator();

// Register listeners to eventGenerators
eventGenerator1.addListener(listener1);
eventGenerator1.addListener(listener2);
eventGenerator1.addListener(listener3);

eventGenerator2.addListener(listener1);
eventGenerator2.addListener(listener2);
eventGenerator2.addListener(listener3);

...

// Eventually remove a listener
eventGenerator2.removeListener(listener2);

...

// When doSomething is invoked all listeners on that eventGenerator are invoked
eventGenerator1.doSomething(event1);  // Dispatched to listener 1, 2 and 3
eventGenerator1.doSomething(event2);  // Dispatched to listener 1, 2 and 3
eventGenerator1.doSomething(event3);  // Dispatched to listener 1, 2 and 3
eventGenerator1.doSomething(event4);  // Dispatched to listener 1, 2 and 3

eventGenerator1.removeListener(listener3);

eventGenerator1.doSomething(event5);  // Dispatched to listener 1, 2

eventGenerator1.removeListener(listener1);

eventGenerator1.doSomething(event6);  // Dispatched to listener 2

我正在做:

Product
Title

我想循环标题的所有结果,但每个分组只显示一次产品:

SELECT * FROM products ORDER BY product

我正在做一个常规的while循环,当然它显示每行的Product列。我知道我以前做过这件事但不能记住我的生活!有任何想法吗?感谢。

1 个答案:

答案 0 :(得分:1)

由于您已按产品订购结果,因此可以执行以下操作:

$currentProduct = null;

foreach($products as $product) {

    if ($currentProduct != $product['product']) {
        // We got a new product. Show it with some fancy html
        $currentProduct = $product['product'];
    }

    // Show the title with some fancy html
}

如果您想使用现有的do while - 循环,那就相同了。