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

----------+----+-----------+----------------------
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
----------+----+-----+-----+----------------------


我知道这是通过循环完成的,但无法解决。谢谢!

最佳答案

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

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

10-06 07:47