如何调整此代码以便从生成的列表中删除第一个ul标记?甚至有可能这样做吗?
这将是我想得到的结果:
<ul> <--remove this initial tag
<li></li>
<li></li>
</ul>
<ul>
<li></li>
<li></li>
</ul>
列表生成器
function buildMenu($parent, $menu)
{
$menulist = "";
if (isset($menu['parents'][$parent]))
{
$menulist .= "<ul>\n";
foreach ($menu['parents'][$parent] as $itemId)
{
if(!isset($menu['parents'][$itemId]))
{
$menulist .= " <li><a href='".$menu['items'][$itemId]['menuURL']."'>".$menu['items'][$itemId]['menuName']."</a></li> \n";
}
if(isset($menu['parents'][$itemId]))
{
$menulist .= " <li><a href='".$menu['items'][$itemId]['menuURL']."'>".$menu['items'][$itemId]['menuName']."</a>";
$menulist .= buildMenu($itemId, $menu);
$menulist .= "</li> \n";
}
}
$menulist .= "</ul> \n";
}
return $menulist;
}?>
答案 0 :(得分:1)
设置分隔符。注意$ first添加到函数变量中,然后FALSE表示每次向底部调用
function buildMenu($parent, $menu, $first = TRUE)
{
$menulist = "";
if (isset($menu['parents'][$parent]))
{
$menulist .= $first ? "" : "<ul>\n";
foreach ($menu['parents'][$parent] as $itemId)
{
if(!isset($menu['parents'][$itemId]))
{
$menulist .= " <li><a href='".$menu['items'][$itemId]['menuURL']."'>".$menu['items'][$itemId]['menuName']."</a></li> \n";
}
if(isset($menu['parents'][$itemId]))
{
$menulist .= " <li><a href='".$menu['items'][$itemId]['menuURL']."'>".$menu['items'][$itemId]['menuName']."</a>";
$menulist .= buildMenu($itemId, $menu, FALSE);
$menulist .= "</li> \n";
}
}
$menulist .= "</ul> \n";
}
return $menulist;
}?>
答案 1 :(得分:-1)
替换
$menulist .= "<ul>\n";
通过
$menulist .= strlen($menulist) ? "<ul>\n":"";