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

时间:2014-02-15 17:49:03

标签: php mysql

我想根据$ 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>";
        } ?>

1 个答案:

答案 0 :(得分:1)

这样的事情应该有效。

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

}