如何在 PHP 中列出与 while 循环相同的 id 数据? [英] How can i list has same id data with while loop in PHP?

查看:23
本文介绍了如何在 PHP 中列出与 while 循环相同的 id 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 mysql 表.像这样的专栏

I have a mysql table. column like that

series_id, series_color, product_name

我应该列出与组相同的 series_id 产品.

I should list same series_id product like with group.

我希望像那个列表一样,所有相同的 saries_id 都像这样显示我的屏幕

I want like that list all same saries_id echo my screen like that

A12 Series Product

 - Milk  
 - Tea 
 - sugar
 - water

B12 Series Product

 - Water 
 - Banana 
 - Cofee 
 - Tea

推荐答案

series_id 对结果进行排序,这样所有具有相同值的产品将放在一起.

Order your results by series_id, so all the products with the same value will be together.

$stmt = $pdo->prepare("SELECT series_id, product_name
                       FROM yourTable
                       ORDER BY series_id");
$stmt->execute();

然后在显示结果时,显示系列标题并在更改时开始一个新的

    :

    Then when displaying the results, show the Series header and start a new <ul> whenever it changes:

    $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";
    

    这篇关于如何在 PHP 中列出与 while 循环相同的 id 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆