PHP 递归函数,将站点导航创建为嵌套列表,但没有不必要的菜单项

发布于 2024-09-12 16:12:50 字数 1399 浏览 8 评论 0原文

我真的很喜欢使用递归函数来构建我的网站菜单,但我遇到了一个问题,而且已经困扰了我很多年了。 我需要菜单函数返回嵌套列表,但我不希望显示树的非活动 irelevent 元素。

细节。我有一个 MySql 数据库,其中有一个名为 menu_items 的表,该表存储导航项的所有常用字段(目标、link_text、标题等)以及每个项目的唯一 id,重要的是parent_id。

但这都是有争议的,例如将这些信息存储在 XML 文件中会更容易吗?

例如,这里是一个显示所有元素的示例菜单:

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

但是,如果当前页面是 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>

显然,我会将当前页面的 ID 或名称传递给菜单功能。

有人有什么想法吗? 我已经用头撞墙有一段时间了:-)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2024-09-19 16:12:50

这是我的完整方法,其中数据库有一个页面 ID、一个父 ID、一个链接(永久链接)和一个标题;希望有帮助。

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文