我正在尝试创建一个动态嵌套菜单,并遇到一些我不知道如何处理的问题。在每个顶级菜单项之后,我得到一个空的UL标签集。 此外,当存在下拉菜单时,LI标签集未正确包装UL标签。
这是我试图修改的代码....谢谢!
$sql = "SELECT menuID, menuTitle, menuURL, menuParentID, menuOrderID FROM menu ORDER BY menuParentID, menuOrderID ASC";
$items = mysql_query($sql);
while ($obj = mysql_fetch_object($items)) {
if ($obj->menuParentID == 0) {
$parent_menu[$obj->menuID]['menuTitle'] = $obj->menuTitle;
$parent_menu[$obj->menuID]['link'] = $obj->menuURL;
} else {
$sub_menu[$obj->menuID]['parent'] = $obj->menuParentID;
$sub_menu[$obj->menuID]['menuTitle'] = $obj->menuTitle;
$sub_menu[$obj->menuID]['link'] = $obj->menuURL;
if (!isset($parent_menu[$obj->menuParentID]['count'])) {
$parent_menu[$obj->menuParentID]['count'] = 0;
}
$parent_menu[$obj->menuParentID]['count']++;
}
}
mysql_free_result($items);
function dyn_menu_folded($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "dropdown-menu", $dd_style = "dropdown") {
$menu = "<ul class=\"".$main_id."\">\n";
foreach ($parent_array as $pkey => $pval) {
if (!empty($pval['count'])) {
$menu .= " <li class=\"".$dd_style."\"><a href=\"".$pval['link']./*"?".$qs_val."=".$pkey.*/"\">".$pval['menuTitle']."<b class=\"caret\"></b></a></li>\n";
} else {
$menu .= " <li><a href=\"".$pval['link']."\">".$pval['menuTitle']."</a></li>\n";
}
//if (!empty($_REQUEST[$qs_val])) {
$menu .= "<ul class=\"".$sub_id."\">\n";
foreach ($sub_array as $sval) {
if ($pkey == $sval['parent']) { //
$menu .= "<li><a href=\"".$sval['link']."\">".$sval['menuTitle']."</a></li>\n";
}
}
$menu .= "</ul>\n";
//}
}
$menu .= "</ul>\n";
return $menu;
}
?>
<?php
echo dyn_menu_folded($parent_menu, $sub_menu, "menu", "nav", "dropdown-menu");
?>
//this is a sample of the result.
<ul class="nav">
<li ><a href="#">ITEM A</a></li>
<ul class="dropdown-menu"></ul> //IMPROPER UL TAGS
<li ><a href="#">ITEM B</a></li>
<ul class="dropdown-menu"></ul>
<li ><a href="#">ITEM C</a></li>
<ul class="dropdown-menu"></ul>
<li ><a href="#">ITEM D</a></li>
<ul class="dropdown-menu"></ul>
<li class="dropdown"><a href="#">ITEM E<b class="caret"></b></a>
</li> //LI TAG NOT WRAPPING AROUND UL TAG
<ul class="dropdown-menu">
<li ><a href="#">ITEM E1</a></li>
<li ><a href="#">ITEM E2</a></li>
<li ><a href="#">ITEM E3</a></li>
<li ><a href="#">ITEM E4</a></li>
<li ><a href="#">ITEM E5</a></li>
</ul>
<li class="dropdown"><a href="#">ITEM F<b class="caret"></b></a>
</li>
<ul class="dropdown-menu">
<li ><a href="#">ITEM F1</a></li>
<li ><a href="#">ITEM F2</a></li>
<li ><a href="#">ITEM F3</a></li>
<li ><a href="#">ITEM F4</a></li>
<li ><a href="#">ITEM F5</a></li>
<li ><a href="#">ITEM F6</a></li>
</ul>
<li ><a href="#">ITEM G</a></li>
<ul class="dropdown-menu"></ul>
<li ><a href="#">ITEM H</a></li>
<ul class="dropdown-menu"></ul>
<li ><a href="#">ITEM I</a></li>
<ul class="dropdown-menu"></ul>
<li ><a href="#">ITEM J</a></li>
<ul class="dropdown-menu"></ul>
</ul>
//This is how it should look.
<ul class="nav">
<li ><a href="#">ITEM A</a></li>
<li ><a href="#">ITEM B</a></li>
<li ><a href="#">ITEM C</a></li>
<li ><a href="#">ITEM D</a></li>
<li class="dropdown"><a href="#">ITEM E<b class="caret"></b></a>
<ul class="dropdown-menu">
<li ><a href="#">ITEM E1</a></li>
<li ><a href="#">ITEM E2</a></li>
<li ><a href="#">ITEM E3</a></li>
<li ><a href="#">ITEM E4</a></li>
<li ><a href="#">ITEM E5</a></li>
</ul>
</li>
<li class="dropdown"><a href="#">ITEM F<b class="caret"></b></a>
<ul class="dropdown-menu">
<li ><a href="#">ITEM F1</a></li>
<li ><a href="#">ITEM F2</a></li>
<li ><a href="#">ITEM F3</a></li>
<li ><a href="#">ITEM F4</a></li>
<li ><a href="#">ITEM F5</a></li>
<li ><a href="#">ITEM F6</a></li>
</ul>
</li>
<li ><a href="#">ITEM G</a></li>
<li ><a href="#">ITEM H</a></li>
<li ><a href="#">ITEM I</a></li>
<li ><a href="#">ITEM J</a></li>
</ul>
答案 0 :(得分:0)
如果不确定$ parent_menu和$ sub_menu是如何构建的,我不能确定这是否可以完成这项工作,但无论如何都要试一试。
function create_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "dropdown-menu", $dd_style = "dropdown")
{
$menu = "<ul class=\"{$main_id}\">\n";
foreach ($parent_array as $pkey => $pval) {
if ( ! empty($pval['count'])) {
$menu .= "\t<li class=\"{$dd_style}\"><a href=\"{$pval['link']}\">{$pval['menuTitle']}<b class=\"caret\"></b></a>\n";
$menu .= "\t\t<ul class=\"{$sub_id}\">\n";
foreach ($sub_array as $sval) {
if ($pkey == $sval['parent']) {
$menu .= "\t\t\t<li><a href=\"{$sval['link']}\">{$sval['menuTitle']}</a></li>\n";
}
}
$menu .= "</ul></li>\n";
} else {
$menu .= "\t<li><a href=\"{$pval['link']}\">{$pval['menuTitle']}</a></li>\n";
}
}
return $menu . "</ul>\n";
}
如果您仍有问题,请发布$ parent_menu和$ sub_menu的var_dump()输出。