开发者

PHP tree menu nested lists problem

My code for some reason is displaying my nested lists for my tree menu all wrong can someone help me correct this problem? So that my nested lists are nested correctly for my tree menu.

Here is my PHP code.

function category_tree($parent = 0, $parent_url = ''){
    echo "<ol>";
    $mysqli = mysqli_connect("localhost", "root", "", "sitename");
    $q = "SELECT id, category, 开发者_开发问答url FROM categories WHERE parent_id = '" . $parent . "' ORDER BY category asc";
    $r = mysqli_query($mysqli, $q);
    while($rs = mysqli_fetch_array($r) ){
        $url = $parent_url . $rs['url'];
        echo '<li><a href="' . $url . '" title="' . $rs['category'] . ' Category Link">' . $rs['category'] . '</a></li>';
        category_tree($rs['id'], $url);
    }
    mysqli_free_result($r);
    echo "</ol>";
}


Without looking further, I'd say you need to call category_tree before echoing the closing tag of the LI element:

    echo '<li><a href="' . $url . '" title="' . $rs['category'] . ' Category Link">' . $rs['category'] . '</a>';
    category_tree($rs['id'], $url);
    echo '</li>';


Most probably no of active resultset goes too many.

Free the result set before recursive call. Populate all rows of a resultset to a PHP $array. Then Free that resultset. Then use a loop to print tree elements(as u r doing) and to find further child tree. It could help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜