如何创建“多维树菜单”使用PHP和SQL? [英] How to create "multidimensional tree menu" using PHP and SQL?
问题描述
我搜索了一段时间,但在google和其他论坛找不到任何东西。但它是奇怪的,因为它是我们大学的流行任务。
我有一个任务:
创建多维树菜单(深度未定义,可以是4或7,取决于用户的意愿),应该有添加新元素,编辑,删除和显示整个树结构使用PHP,SQL和最小CSS p>
树的视觉示例:
+菜单
pre>
+ Shop
+ Tshirts
+ Yellow
+ Green
+裤子$ b $ b +论坛
+想法
您可以看到它有4个深度级别:
Menu-> Shop-> tshirt->黄色
但是我必须让用户可以添加他想要的级别和元素。
有任何例子, SQL结构应该保持?
谢谢!解决方案您想要使用ID和parentID来保存DB中的每个元素如果没有这样的父存在,则为null)。
考虑下面的数据库结果:
----------------------------
| id | parentID |文本|
| ---- | ---------- | ---------- |
| 1 | null |项目#1 |
| 2 | 5 |项目#2 |
| 3 | 2 |项目#3 |
| 4 | 2 |项目#4 |
| 5 | null |项目#5 |
| 6 | 5 |项目#6 |
| 7 | 3 |项目#7 |
| 8 | 5 |项目#8 |
| 9 | 1 |项目#9 |
| 10 | 7 |项目#10 |
----------------------------
考虑下面的数组(可能来自一个数据库结果 - 重要的是,ID是键,你可以简单地将你的数据库结果转换为类似下面的需要的键是parentID):
$ menu = array(
1 = ='Item#1','parentID'=> null),
2 => array('text'=>'Item#2','parentID'=> 5) b $ b 3 => array('text'=>'Item#3','parentID'=> 2),
4 => array('text'=& ','parentID'=> 2),
5 => array('text'=>'Item#5' array('text'=>'Item#6','parentID'=> 5),
7 => array('text'=>'Item#7','parentID'=> ; 3),
8 => array('text'=>'Item#8','parentID'=> 5),
9 = ;'Item#9','parentID'=> 1),
10 => array('text'=>'Item#10','parentID'=& 7),
);
并将其变成树形结构:
<?php
$ addedAsChildren = array();
foreach($ menu = $ id =>& $ menuItem){//注意我们使用引用,所以我们不要重复数组
if(!empty menuItem ['parentID'])){
$ addedAsChildren [] = $ id; //它应该从根中删除,但我们会这样做后
if(!isset($ menu [$ menuItem ['parentID']] ['children'])){
$ menu [$ menuItem ['parentID']] ['children'] = array($ id =>& $ menuItem); //&意味着我们使用REFERENCE
} else {
$ menu [$ menuItem ['parentID']] ['children'] [$ id] =& $ menuItem; //&意味着我们使用REFERENCE
}
}
unset($ menuItem ['parentID']); //我们不再需要parentID
}
unset($ menuItem); // unset the reference
foreach($ addedAsChildren as $ itemID){
unset($ menu [$ itemID]); //从根中删除它,所以它只在['children']子阵列中
}
使用这个新数组,我们可以使用简单的递归函数在
ul..li
中输出:echo makeTree($ menu);
function makeTree($ menu){
$ tree ='< ul>';
foreach($ menu as $ id => $ menuItem){
$ tree。='< li>'。 $ menuItem ['text'];
if(!empty($ menuItem ['children'])){
$ tree。= makeTree($ menuItem ['children']);
}
$ tree。='< / li>';
}
return $ tree。 '< / ul>';
}
导致:
< ul>< li>第1项< ul>< li>第9项< / li>< / ul>< / li>项目#5< ul>< li>项目#2< ul>< li>项目#3< ul>项目#7< li>项目#10< / li> < / ul>< / li>< li>项目#6< / li>< / ul>< / li>< / li>< ; li>项目#8< / li>< / ul>< / li>< / ul&
..并呈现:
img src =https://i.stack.imgur.com/8q5UR.pngalt =enter image description here>
I searched for awhile, but couldn't find anything on google and other forums. But it's weird because it's popular task in our university. So I think this post might help other too with same issue.
I got a task:
"Create multidimensional tree menu (depth is undefined, could be 4 or 7, depending on user's will), there should be options like adding new element, editing, deleting and showing the whole tree structure. Using PHP, SQL and minimal CSS."
Visual example of a tree:
+Menu +Shop +Tshirts +Yellow +Green +Pants +Forum +Ideas
As you can see it's 4 depth levels:
Menu->Shop->tshirt->yellow
But I have to make it so that user could add as many levels as he wants and elements to it.Is there any examples to it and what SQL structure should I keep on? Thank you!
解决方案You want to save each element in the DB with an ID and a parentID (that can be null if no such parent exists). The PHP is your "biggest" problem, but references are your huge friend here for turning a flat structure into a tree structure.
Consider the following DB result:
---------------------------- | id | parentID | text | |----|----------|----------| | 1 | null | Item #1 | | 2 | 5 | Item #2 | | 3 | 2 | Item #3 | | 4 | 2 | Item #4 | | 5 | null | Item #5 | | 6 | 5 | Item #6 | | 7 | 3 | Item #7 | | 8 | 5 | Item #8 | | 9 | 1 | Item #9 | | 10 | 7 | Item #10 | ----------------------------
Consider the following array (that could be from a DB result - it's important that the ID is the key, though. You can simply transform your DB result into something like the following (the only needed key is "parentID"):
$menu = array( 1 => array('text' => 'Item #1', 'parentID' => null), 2 => array('text' => 'Item #2', 'parentID' => 5), 3 => array('text' => 'Item #3', 'parentID' => 2), 4 => array('text' => 'Item #4', 'parentID' => 2), 5 => array('text' => 'Item #5', 'parentID' => null), 6 => array('text' => 'Item #6', 'parentID' => 5), 7 => array('text' => 'Item #7', 'parentID' => 3), 8 => array('text' => 'Item #8', 'parentID' => 5), 9 => array('text' => 'Item #9', 'parentID' => 1), 10 => array('text' => 'Item #10', 'parentID' => 7), );
And to turn it into a tree structure:
<?php $addedAsChildren = array(); foreach ($menu as $id => &$menuItem) { // note that we use a reference so we don't duplicate the array if (!empty($menuItem['parentID'])) { $addedAsChildren[] = $id; // it should be removed from root, but we'll do that later if (!isset($menu[$menuItem['parentID']]['children'])) { $menu[$menuItem['parentID']]['children'] = array($id => &$menuItem); // & means we use the REFERENCE } else { $menu[$menuItem['parentID']]['children'][$id] = &$menuItem; // & means we use the REFERENCE } } unset($menuItem['parentID']); // we don't need parentID any more } unset($menuItem); // unset the reference foreach ($addedAsChildren as $itemID) { unset($menu[$itemID]); // remove it from root so it's only in the ['children'] subarray }
With this new array we can use a simply recursive function to output it all in a
ul..li
sense:echo makeTree($menu); function makeTree($menu) { $tree = '<ul>'; foreach ($menu as $id => $menuItem) { $tree .= '<li>' . $menuItem['text']; if (!empty($menuItem['children'])) { $tree .= makeTree($menuItem['children']); } $tree .= '</li>'; } return $tree . '</ul>'; }
Resulting in:
<ul><li>Item #1<ul><li>Item #9</li></ul></li><li>Item #5<ul><li>Item #2<ul><li>Item #3<ul><li>Item #7<ul><li>Item #10</li></ul></li></ul></li><li>Item #4</li></ul></li><li>Item #6</li><li>Item #8</li></ul></li></ul>
..and rendered:
这篇关于如何创建“多维树菜单”使用PHP和SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!