带PHP / MySQL的多级菜单 [英] Multi level menu with PHP/MySQL
问题描述
我试图用PHP创建从MySQL数据库中获取数据的动态多级菜单。我设法以这种格式在php数组中排列菜单项:
--------- --------------
数组
(
[1] =>数组
(
[id] => 1
[ubicacion] => top_a
[nivel] => 1
[parent_id] =>
[tipo] => link
[link ] => http://www.google.com
[titulo] => Google
[别名] => google_es
[children] => Array
(
[3] =>数组
(
[id] => 3
[ubicacion] => top_a
[nivel] => 2
[parent_id] => 1
[tipo] => link
[link] => http://www.gmail.com
[titulo] => ; Gmail
[别名] => gmail
[孩子ren] =>数组
(
[4] =>数组
(
[id] => 4
[ubicacion] => top_a
[nivel ] => 3
[parent_id] => 3
[tipo] => link
[link] => www.inbox.gmail.com
[titulo ] =>收件箱
[别名] => inbox_gmail
)
)
)
)
)
[2] =>数组
(
[id] => 2
[ubicacion] => top_a
[nivel] => 1
[parent_id] =>
[tipo] => link
[link] => http://www.yahoo.com
[titulo] => Yahoo
[别名] =>雅虎
)
)
-----------------------
问题是,我无法弄清楚如何将这个数组作为HTML标记以n级的方式输出。我可以用这样的固定数量来完成它:
foreach($ menu_array as $ menu){
回声< li>< a href ='{$ menu ['link']}'> {$ menu ['titulo']}< / a>;
if(array_key_exists('children',$ menu)){
echo< ul>;
foreach($ menu ['children'] as $ child_menu){
echo< li>< a href ='{$ child_menu ['link']}'> {$ child_menu [ 'TITULO']}< / A>中;
if(array_key_exists('children',$ child_menu)){
echo< ul>;
foreach($ child_menu ['children'] as $ child2_menu){
echo< li>< a href ='{$ child2_menu ['link']}'> {$ child2_menu [ 'TITULO']}< / A>中;
}
echo< / ul>;
}
}
echo< / ul>;
}
回显< / li>;
}
但是这只适用于3个级别,我知道应该有一种方法为了解决这个问题,我知道我不是第一个面临多维数组HTML输出问题的人。
解决方案您可以使用一点递归来获得更多关卡。
函数echo_menu($ menu_array){
//遍历每个顶级菜单项
foreach($ menu_array as $ menu){
echo< li>< a href ='{$ menu ['link']}'> ; {$菜单[ 'TITULO']}< / A>中;
//查看这个菜单是否有子元素
if(array_key_exists('children',$ menu)){
echo'< ul>';
//回显子菜单
echo_menu($ menu ['children']);
echo'< / ul>';
}
echo'< / li>';
}
}
echo'< ul>';
echo_menu($ menu_array);
echo'< / ul>';
这适用于任何您想要的子级别。
I'm trying to create dynamic multi level menus fetching the data from a MySQL DB, using PHP. I've managed to order the menu items in a php array with this format:
-----------------------
Array
(
[1] => Array
(
[id] => 1
[ubicacion] => top_a
[nivel] => 1
[parent_id] =>
[tipo] => link
[link] => http://www.google.com
[titulo] => Google
[alias] => google_es
[children] => Array
(
[3] => Array
(
[id] => 3
[ubicacion] => top_a
[nivel] => 2
[parent_id] => 1
[tipo] => link
[link] => http://www.gmail.com
[titulo] => Gmail
[alias] => gmail
[children] => Array
(
[4] => Array
(
[id] => 4
[ubicacion] => top_a
[nivel] => 3
[parent_id] => 3
[tipo] => link
[link] => www.inbox.gmail.com
[titulo] => Inbox
[alias] => inbox_gmail
)
)
)
)
)
[2] => Array
(
[id] => 2
[ubicacion] => top_a
[nivel] => 1
[parent_id] =>
[tipo] => link
[link] => http://www.yahoo.com
[titulo] => Yahoo
[alias] => yahoo
)
)
-----------------------
The problem is that I can't figure out how to output this array as HTML markup in a way that will work with n levels. I can do it with a fixed number of levels like this:
foreach($menu_array as $menu) {
echo "<li><a href='{$menu['link']}'>{$menu['titulo']}</a>";
if (array_key_exists('children',$menu)) {
echo "<ul>";
foreach ($menu['children'] as $child_menu) {
echo "<li><a href='{$child_menu['link']}'>{$child_menu['titulo']}</a>";
if (array_key_exists('children',$child_menu)) {
echo "<ul>";
foreach ($child_menu['children'] as $child2_menu) {
echo "<li><a href='{$child2_menu['link']}'>{$child2_menu['titulo']}</a>";
}
echo "</ul>";
}
}
echo "</ul>";
}
echo "</li>";
}
But this only works for 3 levels, and I know there should be a way to solve this issue, I know I'm not the first one facing a problem with HTML output of a multidimensional array.
You can just use a little bit of recursion to get you to more levels.
function echo_menu($menu_array) {
//go through each top level menu item
foreach($menu_array as $menu) {
echo "<li><a href='{$menu['link']}'>{$menu['titulo']}</a>";
//see if this menu has children
if(array_key_exists('children', $menu)) {
echo '<ul>';
//echo the child menu
echo_menu($menu['children']);
echo '</ul>';
}
echo '</li>';
}
}
echo '<ul>';
echo_menu($menu_array);
echo '</ul>';
This will work for any number of child levels you'd like.
这篇关于带PHP / MySQL的多级菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!