gpt4 book ai didi

php - 通过 SQL 查询在 php 中按 header 分组

转载 作者:行者123 更新时间:2023-11-29 06:03:11 24 4
gpt4 key购买 nike

这是我从查询中获得的表格:

----------+----+-----------+----------------------
45678-sm-w| 18 |T-Shirts | Mens clothing
----------+----+-----------+----------------------
45678-sm-b| 5 |T-Shirts | Mens clothing
----------+----+-----------+----------------------
2189-L-Wh | 4 | Socks | Juniors Clothing
----------+----+-----------+----------------------
2189-L-Bl | 3 | Socks | Juniors Clothing
----------+----+-----+-----+----------------------

我想将如下内容放入报告中:

T-Shirts
----------+----+-----------+----------------------
45678-sm-w| 18 |T-Shirts | Mens clothing
----------+----+-----------+----------------------
45678-sm-b| 5 |T-Shirts | Mens clothing
----------+----+-----------+----------------------

Socks
----------+----+-----------+----------------------
2189-L-Wh | 4 | Socks | Juniors Clothing
----------+----+-----------+----------------------
2189-L-Bl | 3 | Socks | Juniors Clothing
----------+----+-----+-----+----------------------

我知道它是通过循环完成的,但无法弄清楚。谢谢!

最佳答案

尝试按项目类型对查询进行排序,并在当前 header 更改时强制使用 header 。

$res = mysql_query("select * from clothing order by item_type, item_size");

$item_type=null;
while ($row = mysql_fetch_assoc($res)) {
if ($item_type != $row['item_type']) {
$item_type = $row['item_type'];
echo $item_type . "\r\n";
}
echo $row["item_name"];
echo $row["item_size"];
echo $row["item_desc"];
}

关于php - 通过 SQL 查询在 php 中按 header 分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12866112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com