【问题标题】:Print categories from a database从数据库打印类别
【发布时间】:2014-07-02 10:45:00
【问题描述】:

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

数据库:

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>

【问题讨论】:

  • 不要使用 mysql_* 函数。使用 mysqli_*。也用这个$result1=mysql_query($sql1,$conn);替换这个$result1=mysql_query($sql,$conn);

标签: php database printing categories


【解决方案1】:

你正在执行相同的 sql 两次。

更改(第 9 行):

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多