将产品精确地显示在其所属类别的下方

我想创建一个“php-mysql”系统,管理员可以在其中创建类别和产品。

产品将动态存储在它们所属的类别中,并且在用户界面上,这些产品将显示在它们所属的类别的正下方。

例如:现在我的产品显示如下图所示:

http://img1.mukewang.com/631ebdab000162fd05710271.jpg

我希望它们显示如下图所示:

http://img2.mukewang.com/631ebdb600018d6005070268.jpg

我的我的我的表是:

http://img3.mukewang.com/631ebdc00001b04606510259.jpg

到目前为止,我的代码(sql)是:


<?php

    function getCategory() {

        $db_conn = getConnection();

        if (!$db_conn) return false;


        $sql = "SELECT category from products";

        $result = $db_conn->query($sql);

        $db_conn->close();

        return $result;

    }


    function getProduct() {

        $db_conn = getConnection();

        if (!$db_conn) return false;


        $sql = "SELECT * from products";

        $result = $db_conn->query($sql);

        $db_conn->close();

        return $result;

    }

?>

断续器代码:


<div class="category">

    <?php $categories = getCategory(); ?>

    <?php while ($category = $categories->fetch_assoc()) : ?>

    <h2><?php echo $category['name']; ?></h2>

    <div class="products">

        <?php $products = getProduct(); ?>

        <?php while ($product = $products->fetch_assoc()): ?>

        <table>

            <tr>

                <td><?php echo $category['name']; ?></td>

            </tr>

            <tr>

                <td><?php echo $category['image']; ?></td>

            </tr>

            <tr>

                <td><?php echo $category['price']; ?></td>

            </tr>

        </table>


Helenr
浏览 63回答 2
2回答

海绵宝宝撒

为了显示每个类别的所有产品,您需要在使用第二个函数获取产品时传递类别名称:<?php&nbsp; &nbsp; function getCategory() {&nbsp; &nbsp; &nbsp; &nbsp; $db_conn = getConnection();&nbsp; &nbsp; &nbsp; &nbsp; if (!$db_conn) return false;&nbsp; &nbsp; &nbsp; &nbsp; $sql = "SELECT DISTINCT category from products";&nbsp; &nbsp; &nbsp; &nbsp; $result = $db_conn->query($sql);&nbsp; &nbsp; &nbsp; &nbsp; return $result;&nbsp; &nbsp; }&nbsp; &nbsp; function getProduct($category) {&nbsp; &nbsp; &nbsp; &nbsp; $db_conn = getConnection();&nbsp; &nbsp; &nbsp; &nbsp; if (!$db_conn) return false;&nbsp; &nbsp; &nbsp; &nbsp; $sql = "SELECT * from products WHERE category = '" . $category . "'";&nbsp; &nbsp; &nbsp; &nbsp; $result = $db_conn->query($sql);&nbsp; &nbsp; &nbsp; &nbsp; return $result;&nbsp; &nbsp; }?>输出:<?php $categories = getCategory(); ?><?php while ($category = $categories->fetch_assoc()) : ?>&nbsp; &nbsp; <div class="category">&nbsp; &nbsp; &nbsp; &nbsp; <h2><?php echo $category['name']; ?></h2>&nbsp; &nbsp; &nbsp; &nbsp; <div class="products">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php $products = getProduct($category['name']); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php while ($product = $products->fetch_assoc()): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $product['name']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><img src="<?php echo $product['image']; ?>"/></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $product['price']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endwhile; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div><?php endwhile; ?>

慕尼黑的夜晚无繁华

SELECT * FROM products ORDER BY category然后只需检查自您输出的最后一条记录以来类别是否已更改,如果有,则显示类别名称,如果没有,则回显该记录。如果您这样做,也不需要第一个查询。另一种方法是,保留第一个查询,但将第二个查询更改为:'SELECT&nbsp;*&nbsp;FROM&nbsp;products&nbsp;WHERE&nbsp;category&nbsp;=&nbsp;'&nbsp;.&nbsp;$category其中类别来自您的第一个查询。
打开App,查看更多内容
随时随地看视频慕课网APP