简体   繁体   English

从数据库打印类别

[英]Print categories from a database

I'm trying to dynamically populate a drop down menu with categories and subcategories from a database table. 我正在尝试使用数据库表中的类别和子类别动态填充下拉菜单。 When I test the code below, I get only father categories: 当我测试下面的代码时,我只会得到父类别:

Database: 数据库:

id_cat //category id
nom_cat //category name
pere // category father 

HTML: HTML:

<div id='cssmenu'>
<ul>
    <?php
        $sql="select * from categories where (pere=0) ";
        $result=mysql_query($sql,$conn);
        while($rows = mysql_fetch_array($result))
        {
        $sql1="select * from categories where (pere=$rows[0]) ";
        $result1=mysql_query($sql,$conn);
        $nc =mysql_num_rows($result1);
        if($nc>0){$ctype='has-sub';}else{$ctype='';}
        echo"<li class='".$ctype."'><a href='#'><span>".$rows[1]."</span></a></li>";
        if ($nc>0){
            echo"<ul>";
            while($rows1 = mysql_fetch_array($result1))
            {
                echo"<li><a href='categories.php?cat=$rows1[0]'><span>$rows1[1]</span></a></li>";
            }
            echo"</ul>";
            }
        }
        echo"</ul>";
    ?>
</div>

You are executing the same sql twice. 您执行两次相同的SQL。

Change (line 9): 更改(第9行):

$result1=mysql_query($sql,$conn);

to

$result1=mysql_query($sql1,$conn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM