views:

44

answers:

0

I really dig the idea of using a recursice function to build my site menus but I am having a problem and have been banging my head for ages now. I need my menu function to return a nested list but I dont want non-active irelevent elements of the tree to be displayed.

Details. I have a MySql database with a table called menu_items that stores all of the usual fields for a nav item (target, link_text, title, etc) as well as a unique id for each item and importantly a parent_id.

This is all up for debate though, for instance would it be easier to store this information in an XML file?

For example here is an example menu with all elements shown:

<ul>
    <li><a href="1.html">link 1</a>
        <ul>
            <li><a href="1-1.html">link 1-1</a>
            <li><a href="1-2.html">link 1-2</a>
        </ul>
    </li>
    <li><a href="2.html">link 2</a>
        <ul>
            <li><a href="2-1.html">link 2-1</a>
            <li><a href="2-2.html">link 2-2</a>
        </ul>
    </li>
    <li><a href="3.html">link 3</a></li>
</ul>

But if the current page is for example 1-2.html I want to have a menu like this:

<ul>
    <li><a href="1.html">link 1</a>
        <ul>
            <li><a href="1-1.html">link 1-1</a>
            <li><a href="1-2.html">link 1-2</a>
        </ul>
    </li>
    <li><a href="2.html">link 2</a></li>
    <li><a href="3.html">link 3</a></li>
</ul>

Evidently I would pass either the ID or the name of the current page to the Menu function.

Any ideas anyone? I have been banging my head against a wall for some time now :-)