猿问

我无法在 php 中正确显示菜单?请指教

我想显示这样的菜单

这个菜单应该看起来像

http://img.mukewang.com/62abe6ab0001f3ea12480602.jpg

并且应该是动态的。这意味着如果我们添加新的类别或子类别,那么设计应该是相同的。


// 这是我的代码,我正在使用 PHP


      <ul class="dropdown-menu mega-dropdown-menu row">


        <li class="col-sm-4">

          <ul>

            <?php

            $sql = mysql_query("SELECT * FROM categories WHERE parent_id = '0' ");

            while($res = mysql_fetch_array($sql))

            {

            echo '<li class="dropdown-header">'.$res['cat_name'].'</li>';

            $sql1 = mysql_query("SELECT * FROM categories WHERE parent_id = '".$res['cat_Id']."' ");

            while($res1 = mysql_fetch_array($sql1))

            {

            echo '<li><a href="products.php?subCatId='.$res1['cat_Id'].'">'.$res1['cat_name'].'</a></li>';

            }


            }

            ?>


          </ul>

        </li>


      </ul>


桃花长相依
浏览 92回答 2
2回答

30秒到达战场

您的菜单功能:function getMenuTree($parent_id)&nbsp;{&nbsp; &nbsp; global $con;&nbsp; &nbsp; $menu = "";&nbsp; &nbsp; $sqlquery = " SELECT * FROM categories WHERE parent_id = '$parent_id'";&nbsp; &nbsp; $res=mysql_query($con,$sqlquery);&nbsp; &nbsp; while($row=mysql_fetch_array($res))&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$menu .="<li><a href='products.php?subCatId='.$row['cat_Id'].''>".$row['cat_name']."</a>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$menu .= "<ul>".getMenuTree($row['cat_Id'])."</ul>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$menu .= "</li>";&nbsp; &nbsp; }&nbsp; &nbsp; return $menu;}你可以在你想要的地方调用函数:getMenuTree('0');

阿波罗的战车

请尝试以下代码。我认为它会为你工作。我<div class="col-sm-4">在第一个 while 循环开始之后添加,并</div>在第一个 while 循环结束之前关闭。此类将类别分为 3 部分。&nbsp;<ul class="dropdown-menu mega-dropdown-menu row">&nbsp; &nbsp; &nbsp; &nbsp; <li class="col-sm-4">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql = mysql_query("SELECT * FROM categories WHERE parent_id = '0' ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($res = mysql_fetch_array($sql))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add below line&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<div class="col-sm-4">';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<li class="dropdown-header">'.$res['cat_name'].'</li>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql1 = mysql_query("SELECT * FROM categories WHERE parent_id = '".$res['cat_Id']."' ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($res1 = mysql_fetch_array($sql1))&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="products.php?subCatId='.$res1['cat_Id'].'">'.$res1['cat_name'].'</a></li>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add below line&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</div>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; </ul>
随时随地看视频慕课网APP
我要回答