从数据库打印类别

时间:2014-07-02 10:45:00

标签: php database printing categories

我正在尝试使用数据库表中的类别和子类别动态填充下拉菜单。 当我测试下面的代码时,我只得到父类:

数据库:

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

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>

1 个答案:

答案 0 :(得分:0)

您正在执行两次相同的SQL。

更改(第9行):

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

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