在菜单上显示子类别

Showing sub-categories on Menu

我的类别 table 是这样的:

id - title - sub

*sub 为 0 表示它是主要类别。

使用下面的代码,我可以列出我的主要类别;

这是代码;

  <?php

function menucategories() {
include ('connect.php');

$q=mysql_query("select  * from categories where sub='0' order by asc");
while ($r=mysql_fetch_array($q))

    echo "<a class='cat' href='cat.php?id=$r[id]'>- $r[title]</a>";
}

?>

但我想这样列出;

-sub 1.1

-sub 1.2

-sub 2.1

-sub 2.2

我是初学者,找不到方法。所以,我在等你的帮助。非常感谢。

试试这个代码

function menucategories() {
include ('connect.php');

$q=mysql_query("select  * from categories where sub='0' order by asc");
while ($r=mysql_fetch_array($q)){

    echo "<a class='cat' href='cat.php?id=$r[id]'>- $r[title]</a>";
    $q1=mysql_query("select  * from categories where sub='".$r[id]."' order by asc");
    while ($r1=mysql_fetch_array($q1)){
        echo "<a class='cat' href='cat.php?id=$r1[id]'>-- $r1[title]</a>";
    }

}