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 m开发者_开发问答odify 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
Well use a hierarchical schema, add a sort order to each item and that's it.
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.
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.
What you are looking for, is called recursion, here is related question example, also I would recommend to read this article.
Exactly what are you wanting to know?
If you are interested in structure, check out this article on hierarchical data on the MySQL website.
精彩评论