简体   繁体   中英

PHP recursive function, to create site navigation as nested list, but without unnecessary menu items

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 :-)

This is my complete method where the databse has a page id, a parent id, a link (permalink) and a title; hope it helps.

function make_nav($current_page_id)
{
    nav_rec(0, $current_page_id, constant('SITE_URL'), 0);
}

function nav_rec($page, $current_page_id, $link, $level)
{   
    if (!$page)
    {
        $page = array();
        $page['page_id'] =  '0';
    }
    else
    {
        ?><a class="nav-link nav-level-<?= $level ?>" href="<?= $link ?>"><?= $page['title'] ?></a><?
    }

    // Checks for the subpages from this page.  
    $page_q = "
        SELECT id AS page_id, parent_id, permalink, page_title AS title 
        FROM ".constant('MYSQL_PREFIX')."pages 
        WHERE `page_type` = 'page'
        AND `in_nav` = '1'
        AND `status` = 'published'
        AND `is_deleted` = '0'
        AND `parent_id` = '".$page['page_id']."'";

    if ($page_rs = mysql_query($page_q))
    {
        if (mysql_num_rows($page_rs))
        {
            while($page = mysql_fetch_assoc($page_rs))
            {
                nav_rec($page, $current_page_id, $link . $page['permalink'].'/', $level+1);
            }
        }
        else
        {
            $level = 0;
        }
    }
    else
    {
        $level = 0;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM