在下拉菜单中显示类别和子类别

时间:2016-11-16 00:33:03

标签: php mysqli

我试图在下拉菜单中显示类别和子类别,我有:

<?php


              $cat_query = "SELECT * FROM `categories`";
              $select_categories = mysqli_query($connection, $cat_query);
              while ($cat = mysqli_fetch_array($select_categories)) {

                $cat_id = $cat['cat_id'];
                $cat_title = $cat['cat_title'];

                 echo "<li>";
                 echo "<a href='#'>$cat_title <span class='caret'></span></a>";

                 $sub_query = "SELECT * FROM `sub_categories` WHERE `cat_id` = $cat_id";
                 $select_sub_categories = mysqli_query($connection, $sub_query);

                 while ($sub = mysqli_fetch_array($select_sub_categories)) {

                  $subcat_id = $sub['subcat_id'];
                  $subcat_title = $sub['subcat_title'];
                  echo "<ul class='dropdown-menu'><li>";
                  echo "<a href='#'>$subcat_title</a></li>";
                  echo "</ul>";
                }

                  echo "</li>";

               }

          ?>

它实际上是从数据库中获取类别,但它没有正确显示。

在桌面版上,它显示如下: Desktop View

在移动版上,它显示如下: Mobile View

我做错了吗? 任何帮助将不胜感激。 感谢

1 个答案:

答案 0 :(得分:0)

既然你没有提供css,javascript或者一些html代码,我认为这个问题就在这个php块中

while ($sub = mysqli_fetch_array($select_sub_categories)) {

              $subcat_id = $sub['subcat_id'];
              $subcat_title = $sub['subcat_title'];
              echo "<ul class='dropdown-menu'><li>";
              echo "<a href='#'>$subcat_title</a></li>";
              echo "</ul>";
            }

从这个代码块看起来你正在创建一个下拉菜单列表,所以也许它应该是

echo "<ul class='dropdown-menu'>";
while ($sub = mysqli_fetch_array($select_sub_categories)) {

              $subcat_id = $sub['subcat_id'];
              $subcat_title = $sub['subcat_title'];
              echo "<li><a href='#'>$subcat_title</a></li>";
 }
echo "</ul>";

如果这不起作用你应该再次检查你的javascript,css,html代码。因为我不了解你的设计方案