开发者

PHP/HTML Nested Navigation list giving unexpected results

I'm writing a recursive navigation menu in PHP for easier organization and expandability. For now I am using some examples from Diablo II to get the nesting to work properly.

The recursive looping is giving me some weird results. Sometimes it only returns a single letter as opposed to the name/url of an array and it doesn't recurse through all elements.

It's a bit difficult to explain, and my mai开发者_StackOverflown issue is I don't really understand how to debug this. It's probably something simple that I can't figure out.

To save everyone time, I made a page with the source (syntax highlighted) and output here:

http://radleygh.com/files/test/example.php

That should make it easy to see what is going on.

The layout of the list should look like this (With the name and UL of each group contained within the same LI element):

ul
  li 
    Parent Title #1
    ul
      li
        Child Title #1
      li 
        Child Title #2
    /ul
  li
    Parent Title #2
/ul

PS: Is there a better way to go about sorting a huge array like the one I have?

Thanks. Back to debugging for now


You were pretty close. Where your mistake lies is within the recurse_array function. You are passing it elements of a single dimension array (I know that it is technically multiple dimensions given $element[2], but the first two elements are single :)), so when it grabs that, it takes that the words are to be broken out into their own array.

Try this:

function recurse_array($element) {
    echo "<ul>\n";
    echo "  <li>"."<a href='{$element[1]}'>{$element[0]}</a>\n";
    if (is_array($element[2])) {
        echo recurse_array($element[2]);
    }
    echo "  </li>\n";
    echo "</ul>";
}

It should give you what you are looking for, or at least something a bit closer to what you are looking for.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜