基于SQL结果生成动态Navbar?

时间:2022-11-24 08:45:39

Hello superiors coders.

你好上级编码员。

Im trying to make a shop section as part of a website which includes a sidebar navbar which can be clicked to specify sections of a product e.g. electrical or cctv. I've already done this by allocating products a category and then to generate the navbar i am querying all the categories and then looping through creating the HTML so the bar displays all the categories

我试图将商店部分作为网站的一部分,其中包括侧栏导航栏,可以单击该栏以指定产品的部分,例如电气或*电视台。我已经通过分配产品类别然后生成导航栏来完成此操作我正在查询所有类别,然后循环创建HTML,以便栏显示所有类别

Example

<div class="leftside" id="leftbar">
<nav class="menu-top">
    <ul class="off-canvas-list" style="font-size: 25px;font-weight: bold">
        <li style="padding-top: 2.7rem"></li>
        <?php foreach ($item_list as $list) {
            echo "<li><a href='/welcome/shop_search/" . $list->type . "'>" . $list->type . "</a></li>";
        }
        ?>
    </ul>
</nav>

The problem is i want to be able to use sub categories but i have not got a clue as to how the SQL data should be set out as well as how to incorporate the intelligence to loop through add the main categories like so and then add dropdown sub categories.

问题是我希望能够使用子类别,但我还没有得到如何设置SQL数据的线索,以及如何将智能结合到循环中添加主要类别,然后添加下拉列表子类别。

+------------+----------------+------+-----+---------+
| ID         | Name           | Main | Sub | Parent  |
+------------+----------------+------+-----+---------+
| 1          | Cabling        |  1   | 0   |         | 
| 2          | network cable  |  0   | 1   | cabling | 
| 2          | electric cable |  0   | 1   | cabling | 
+------------+----------------+------+-----+---------+

this is how I've set up my SQL and I'm aiming to put some sort of logical if statement some how binding the sub categories to the main category so when the loop has finished its creates the navbar with the mains being the buttons and the sub categories in a collapsable inside the relevant main.

这就是我设置SQL的方法,我的目标是将某些逻辑if语句设置为如何将子类别绑定到主类别,这样当循环完成后,它会创建导航栏,主管是按钮和在相关主要内部可折叠的子类别。

1 个解决方案

#1


You have to make another select in each iteration:

您必须在每次迭代中进行另一个选择:

<div class="leftside" id="leftbar">
<nav class="menu-top">
<ul class="off-canvas-list" style="font-size: 25px;font-weight: bold">
    <li style="padding-top: 2.7rem"></li>
    <?php foreach ($item_list as $list) {
        echo "<li><a href='/welcome/shop_search/" . $list->type . "'>" . $list->type . "</a></li>";


        //select * from types where sub=list->type
        //$num_rows = mysql_num_rows($select)
        if($num_rows>0){
            echo '<ul>';
                //for each
                //sub
            echo '</ul>';
        }
    }
    ?>
</ul>

For the design I recomend you the bootstrap navbar

对于设计,我建议你使用bootstrap导航栏

#1


You have to make another select in each iteration:

您必须在每次迭代中进行另一个选择:

<div class="leftside" id="leftbar">
<nav class="menu-top">
<ul class="off-canvas-list" style="font-size: 25px;font-weight: bold">
    <li style="padding-top: 2.7rem"></li>
    <?php foreach ($item_list as $list) {
        echo "<li><a href='/welcome/shop_search/" . $list->type . "'>" . $list->type . "</a></li>";


        //select * from types where sub=list->type
        //$num_rows = mysql_num_rows($select)
        if($num_rows>0){
            echo '<ul>';
                //for each
                //sub
            echo '</ul>';
        }
    }
    ?>
</ul>

For the design I recomend you the bootstrap navbar

对于设计,我建议你使用bootstrap导航栏