0

如何调整此代码以便从生成的列表中删除第一个 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;
    }?>
4

2 回答 2

1

设置分隔符。请注意 $first 添加到函数 vars 中,然后在底部的所有其他调用中添加 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;
}?>
于 2012-07-30T17:16:18.393 回答
-1

代替

$menulist .= "<ul>\n";

经过

$menulist .= strlen($menulist) ? "<ul>\n":"";
于 2012-07-30T17:19:07.797 回答