开发者

PHP nav menu - recursively get subcategories and use <li> and <ul>

Since I need to create a dynamically updated navigation menu, css requires me to structure the data this way:

<li>Projects (Menu Level 1)
    <ul>
        <li>Project 1 (Menu Level 2)</li>
        <li>Project 2 (Menu Level 2)</li>
        <li>Project 3 (Menu Level 2)</li>
    </ul>
</li>
<li>News (Menu Level 1)</li>
<li>Contact (Menu Level 1)</li>

I currently have the data in the following format in an array:

$fore = array(
  '0' => 'NULL',
  '1' => 'element 1 level', // the level in the tree for the following value (0 is for root, 1 is for leaf, 2 is for a leaf of the leaf and so on
  '2' => 'element 1 value', // the actual text that has to appear in the menu
  '3' => 'element 2 level',
  '4' => 'element 2 value'
.......
//and so on
);

I've been trying to do this all day now and I just cant figure out how it should happen. I tried >,< and = and ifs with the levels but there's just too many combinations .. I'll be r开发者_开发知识库eally thankful if someone can help me out here ..


Make your array match the structure of the UI/LI elements so you can iterate through your array and process them in order. You may want to check if the tag changes so you know if you need to echo a closing tag or a new opening tag as you iterate. Right now your structure is so far off you need to place a lot of logic in to decipher between your array and desired output.

This also points back to the source of your array data. If it is coming from a db table then you need to evaluate it's structure also. Look at where it starts and where it ends, then the middle ground should be simple.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜