Php 如何按类别将数据放入表中。

Php 如何按类别将数据放入表中。,php,mysql,Php,Mysql,我想根据$category行将数据放入表中。因此,将有多个表,每个表只包含具有相同$category的项 我已经在这上面呆了一段时间了,所以任何帮助都会很好 提前谢谢 while($row = $result->fetch_array()){ $rows[] = $row; } foreach ($rows as $row) { $food = $row["food"]; $price = $row["price"]; $picture = $ro

我想根据$category行将数据放入表中。因此,将有多个表,每个表只包含具有相同$category的项

我已经在这上面呆了一段时间了,所以任何帮助都会很好

提前谢谢

    while($row = $result->fetch_array()){
    $rows[] = $row;
}

foreach ($rows as $row) {
    $food = $row["food"];
    $price = $row["price"];
    $picture = $row["picture"];
    $id = $row['id'];
    $description = $row['description'];
    $category = $row['category'];

    echo "<tr>
           <td><img src='uploads/$picture' width='120px' height='120'/></td>
           <td><div><span>$food</span></div></td>
           <td><div><span>$$price</span></div></td>
           <td><div><span><input type='number' min='0' max='10' placeholder='#' name='qty[".$id."]' maxlength='1'></span></div></td>
           <td><div><span><i>$description</i></span></div></td>
          </tr>";
        } ?>
while($row=$result->fetch\u array()){
$rows[]=$row;
}
foreach($行作为$行){
$food=$row[“food”];
$price=$row[“price”];
$picture=$row[“picture”];
$id=$row['id'];
$description=$row['description'];
$category=$row['category'];
回声“
美元食品
$$价格
$description
";
} ?>

像这样的东西应该可以用

foreach ($rows as $row) {
    $food = $row["food"];
    $price = $row["price"];
    $picture = $row["picture"];
    $id = $row['id'];
    $description = $row['description'];
    $category = $row['category'];

    $categoryRow = "<tr>
           <td><img src='uploads/$picture' width='120px' height='120'/></td>
           <td><div><span>$food</span></div></td>
           <td><div><span>$$price</span></div></td>
           <td><div><span><input type='number' min='0' max='10' placeholder='#' name='qty[".$id."]' maxlength='1'></span></div></td>
           <td><div><span><i>$description</i></span></div></td>
          </tr>";
    $categoryRows[$category] = $categoryRows[$category] . $categoryRow;
}
foreach($categoryRows as $categoryTitle=>$tableRows){

    echo "<h2>$categoryTitle</h2><table>$tableRows</table>";

}
foreach($rows作为$row){
$food=$row[“food”];
$price=$row[“price”];
$picture=$row[“picture”];
$id=$row['id'];
$description=$row['description'];
$category=$row['category'];
$categoryRow=”
美元食品
$$价格
$description
";
$categoryRows[$category]=$categoryRows[$category]。$categoryRow;
}
foreach($categoryRows为$categoryTitle=>$tableRows){
回显“$categoryTitle$tableRows”;
}

随请求更新。