Mysql
 sql >> Database >  >> RDS >> Mysql

Menu ricorsivo PHP nella struttura dell'elenco HTML

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>';
}