Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Como exibir categorias e subcategorias sem fim usando PHP e MySQL?


Espero que funcione:
function category_tree( $parent = 0, $parent_url = "www.example.com/" ){
    echo "<ol>";
    $sql = sprintf("SELECT id, category, url FROM categories where parent_id = %d order by category asc", $parent);
    $r = mysql_query( $sql );
    while( $rs = mysql_fetch_assoc( $r ) ){
        $url = $parent_url . $rs['url'];
        $item = sprintf("<li> <a href = '%s' >%s</a> </li>", $url , $rs['category']);
        echo $item;
        category_tree( $rs['id'], $url );
    }
    mysql_free_result( $r );
    echo "</ol>";
}