简体   繁体   English

PHP递归功能,以嵌套列表的形式创建网站导航,但没有不必要的菜单项

[英]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. 我需要菜单功能返回嵌套列表,但是我不希望显示树的非活动irelevent元素。

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. 我有一个MySql数据库,其中包含一个名为menu_items的表,该表存储导航项的所有常用字段(目标,link_text,title等),以及每个项的唯一ID,重要的是parent_id。

This is all up for debate though, for instance would it be easier to store this information in an XML file? 但是,所有这些都需要辩论,例如,将这些信息存储在XML文件中会更容易吗?

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: 但是,如果当前页面是例如1-2.html,那么我想要一个像这样的菜单:

<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. 显然,我会将当前页面的ID或名称传递给Menu函数。

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; 这是我的完整方法,其中数据库具有页面ID,父ID,链接(永久链接)和标题。 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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