Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Rekursives PHP-Menü in HTML-Listenstruktur

function listCategory($parent_id,$level=0) {
    $query = "SELECT name, id , parent_id FROM category  WHERE  parent_id=".$parent_id;
    $res = mysql_query($query) or die($query);
    if(mysql_num_rows($res) == 0) return;
    echo '<ul>';
    while (list ($name, $id) = mysql_fetch_row($res))
    {   
        if ($level==0)
        {
           echo '<li><a href="#" class="menulink">'.$name.'</a>';
        }
        else
        {
           echo '<li><a href="#">'.$name.'</a>';
        }
        listCategory($id,$level+1);
        echo '</li>';
    }
    echo '</ul>';
}