如何制作无限动态菜单系统?

发布于 2024-10-17 21:00:40 字数 521 浏览 3 评论 0原文

我有一个 wiki 类型的应用程序,管理员可以在其中创建页面。每个页面都必须放入菜单系统中,该系统由管理员即时创建。

Menu Heading
L Subheading
  L Page1

但是,菜单可能有更多页面,例如:

Menu Heading
L Subheading
  L Page1
  L New Subheading
    L Page2

菜单必须易于修改,并且必须有一种方法来订购菜单标题/副标题以及里面的页面。

所以基本上,我需要一个完全动态的菜单,不需要任何类型的静态数据。

我有一个当前有效的方法,但我无法控制页面顺序。如果我没有得到很多关于如何去做的回复,我会提出我目前的做事方式(我只是不想将想法放入你的脑海中,我正在寻找一种新的方法)

谢谢

编辑:谢谢所有这些都是为了您的答复,但是,我认为情况比我现在可以用言语表达的要复杂一些。让我想一个更好的方式来问我的问题......我可能会在下周重新发布

I have a wiki-type app where an administrator can create pages. Each page must be put into the menu system, which is created on-the-fly by the administrator

Menu Heading
L Subheading
  L Page1

However, there may be more pages for the menu such as:

Menu Heading
L Subheading
  L Page1
  L New Subheading
    L Page2

The menu must be easy to modify and there must be a way to order the headings/subheadings as well as the pages inside.

so basically, an entirely dynamic menu is what i need NO STATIC DATA OF ANY KIND.

I've got a current method that works except i am unable to control page order. If i dont get many responses how to go about it, i will put up my current way of doing things (i just don't want to put ideas into your head, i'm looking for a fresh approach)

Thanks

EDIT: Thank you all for your responses, however, I think the situation is a bit more complex than i can put into words right now. Let me think of a better way to ask my question... I'll repost probably next week

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

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

发布评论

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

评论(5

書生途 2024-10-24 21:00:40

我们将使用 分层架构,为每个项目添加排序顺序,然后就是这样。

Well use a hierarchical schema, add a sort order to each item and that's it.

遗心遗梦遗幸福 2024-10-24 21:00:40

完成具有父级的系统的最简单方法 ->孩子->孩子……关系将是一棵树。您使用的设置可能如下所示:

 id | name      | parent | tree_left | tree_right
----+-----------+--------+-----------+-----------
 0  | root      | NULL   | 0         | 9
 1  | child1    | 0      | 1         | 4
 2  | subchild1 | 1      | 2         | 3
 3  | child2    | 2      | 5         | 8
 4  | subchild2 | 3      | 6         | 7

这将是数据库:

root
|_ child 1
   |_ subchild 1
|_ child 2
   |_ subchild 2

您可以通过两个简单的查询获得完整的结构,如下所示:

SELECT * from menu WHERE name = root;
SELECT * FROM menu WHERE tree_left > $root['tree_left'] AND tree_right <  $root['tree_right'] order by tree_left DESC;

当您添加子项时,您必须扩大父级 tree_left 和 right 之间的间隙。当你在子项、子项等中时,你应该递归地执行此操作。如果我没有记错的话,这可以在 1 个查询中完成。

尝试在网络上搜索更详细的示例,这是一种称为嵌套集的常用树结构。

The easiest way to accomplishing a system which has parent -> child -> child... relations would be a tree. Where you use a setup which could look like this:

 id | name      | parent | tree_left | tree_right
----+-----------+--------+-----------+-----------
 0  | root      | NULL   | 0         | 9
 1  | child1    | 0      | 1         | 4
 2  | subchild1 | 1      | 2         | 3
 3  | child2    | 2      | 5         | 8
 4  | subchild2 | 3      | 6         | 7

This would be the database for:

root
|_ child 1
   |_ subchild 1
|_ child 2
   |_ subchild 2

You can get the full structure in 2 easy querys like so:

SELECT * from menu WHERE name = root;
SELECT * FROM menu WHERE tree_left > $root['tree_left'] AND tree_right <  $root['tree_right'] order by tree_left DESC;

When you add children you must enlarge the gap between your parent tree_left and right. you should do this recursively when your in sub, sub items etc. Which can be done in 1 query if i'm not mistaking.

Try and search on the web for more detailed exampled this is a common used tree structure called nested set.

猫腻 2024-10-24 21:00:40

结构如下的表应该可以做到这一点。

菜单项 ID |父 ID |订单|内容

TopLevelItem1 (MenuItemId: 1)
 |
 |-> MenuItem2 (ParentId: 1, Order 1)
 |-> MenuItem3 (ParentId: 2, Order 2)

TopLevelItem2 (MenuItemId: 4)
 |
 |-> MenuItem4 (ParentId: 4, Order 1)

等。

插入项目应该只需要简单的重新排序。

A table structured as follows should do it.

MenuItemId | ParentId | Order | Content

TopLevelItem1 (MenuItemId: 1)
 |
 |-> MenuItem2 (ParentId: 1, Order 1)
 |-> MenuItem3 (ParentId: 2, Order 2)

TopLevelItem2 (MenuItemId: 4)
 |
 |-> MenuItem4 (ParentId: 4, Order 1)

Etc..

Inserting items should just require a simple reorder.

御守 2024-10-24 21:00:40

您正在寻找的称为递归,这里是相关问题示例,我还建议阅读这篇文章

What you are looking for, is called recursion, here is related question example, also I would recommend to read this article.

浪漫人生路 2024-10-24 21:00:40

您到底想知道什么?

如果您对结构感兴趣,请查看这篇关于分层数据的文章在 MySQL 网站上。

Exactly what are you wanting to know?

If you are interested in structure, check out this article on hierarchical data on the MySQL website.

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