PHP递归函数为空<ul> </ ul>问题

我正在从数据库中构建递归php菜单功能。


我的联系:


 $menu = $vt->prepare("SELECT * FROM menus");

 $menu->execute(array());

 $menuSonuc = $menu->fetchAll(PDO::FETCH_OBJ);

此函数生成空ul。


<li>

 <a href=""><span class="sagok"></span>TITLE</a>

 <ul></ul>

</li>

我的问题:如何防止此功能中的空ul?


echo '<ul class="anamenu">';

           foreach ($menuSonuc as $menum) {





              if ($menum->katID == null) {


                 echo '<li><a href=""><span class="asagiok"></span>' . $menum->baslik . '</a>';

                 $id = $menum->id;

                 kategoriler($menuSonuc, $id);

                 echo '</li>';

              }

           }

           echo '</ul>';



           function kategoriler($menuSonuc, $id)

           {

              echo '<ul>';

              foreach ($menuSonuc as $menum) {

                 if ($menum->katID == $id) {

                    echo '<li><a href=""><span class="sagok"></span>' . $menum->baslik . '</a>';

                    kategoriler($menuSonuc, $menum->id);

                    echo '</li>';

                 }

              }

              echo '</ul>';

           }


MMTTMM
浏览 140回答 2
2回答

梵蒂冈之花

您必须添加一些检查,请参见下面的代码&nbsp; &nbsp; if(!empty($menuSonuc)){&nbsp; &nbsp; echo '<ul class="anamenu">';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;foreach ($menuSonuc as $menum) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($menum->katID == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo '<li><a href=""><span class="asagiok"></span>' . $menum->baslik . '</a>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$id = $menum->id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kategoriler($menuSonuc, $id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo '</li>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; echo '</ul>';}function checkSub($menuSonuc, $id){&nbsp; &nbsp; foreach ($menuSonuc as $menum) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($menum->katID == $id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;return false;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function kategoriler($menuSonuc, $id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!empty($menuSonuc)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $index=0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($menuSonuc as $menum) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($menum->katID == $id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($index==0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<ul>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<li><a href="">'.((checkSub($menuSonuc, $menum->id))?'<span class="sagok"></span>':''). $menum->baslik . '</a>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kategoriler($menuSonuc, $menum->id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</li>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $index++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($index>0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</ul>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

慕丝7291255

如果你空了,ul那就意味着$menuSonuc空了,它不会输入foreach。您可以检查是否为空以防止空ulfunction kategoriler($menuSonuc, $id){if(empty($menuSonuc)){&nbsp; &nbsp; return;}echo '<ul>';foreach ($menuSonuc as $menum) {&nbsp; &nbsp; if ($menum->katID == $id) {&nbsp; &nbsp; &nbsp; &nbsp; echo '<li><a href=""><span class="sagok"></span>' . $menum->baslik . '</a>';&nbsp; &nbsp; &nbsp; &nbsp; kategoriler($menuSonuc, $menum->id);&nbsp; &nbsp; &nbsp; &nbsp; echo '</li>';&nbsp; &nbsp; }}echo '</ul>';}更新如果仍然空着,ul那么问题就在于您正在检查的情况$menum->katID == $id。这意味着它$menum->katID与永远不会相同$id。检查这些值,您将解决您的问题。或者给我们一个您的数组的例子,以便我们检查它们。
打开App,查看更多内容
随时随地看视频慕课网APP