php 菜单和子菜单数组

发布于 2024-11-14 09:02:06 字数 640 浏览 4 评论 0原文

我想要完成的事情:

$config['navigation'] = array( 
        "Home" => "", 
        "Item 1" => array("page1", array()), 
        "Item 2" => array("subpages", array(
            "Item 2.1" => "subpages/page1",
            "Item 2.2" => array("subpages/page2", array()),
            "Item 2.3" => "subpages/page3",
        )), 
        "Item 3" => "another_page", 
        "Contact Us" => array("contact_us", array()), 
);

我的MySql表看起来像这样:

id | name | url | parent

如果填写了父项,则该项目是属于相应id的子菜单。

我的 select 语句和循环应该是什么样子?我想用最少的代码获得最大的结果。

提前致谢..!

What I would like to accomplish:

$config['navigation'] = array( 
        "Home" => "", 
        "Item 1" => array("page1", array()), 
        "Item 2" => array("subpages", array(
            "Item 2.1" => "subpages/page1",
            "Item 2.2" => array("subpages/page2", array()),
            "Item 2.3" => "subpages/page3",
        )), 
        "Item 3" => "another_page", 
        "Contact Us" => array("contact_us", array()), 
);

My MySql table looks something like this:

id | name | url | parent

If parent is filled in the item is a submenu which belongs to the corresponding id.

How should my select statement and loop look like? I would like to achieve a minimum of code for max result.

Thanks in advance..!

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

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

发布评论

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

评论(1

风筝在阴天搁浅。 2024-11-21 09:02:06

像这样的东西可能会起作用...

    select id,name,url,parent from tbl group by parent order by id asc

但是,如果父子关系是可编辑的,您将遇到的问题是菜单的排序..在回家之后和联系我们之前,您可能需要另一个名为 order 的列和可用于对任何给定父母的孩子进行排序

Something like this may work ...

    select id,name,url,parent from tbl group by parent order by id asc

However, the problem you are going to run into if the parent child relationship is editable is the ordering of the menu .. what comes after home and before contact us, you may need another column called order and that could be used to order the children of any given parent

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