不明白为什么类别没有崩溃

时间:2019-02-20 22:28:29

标签: php html database

所以我很困惑,我使用的是模板,并且有一个显示类别的侧菜单,当您在类别上按一个下拉菜单时,该菜单具有与初始类别中ID相关的相应类别。当我按类别时,它并没有给我想要的结果。单击href时,它会为我提供初始类别的结果,而不是与单击的href id对应的类别的结果。不知道我在做什么错? current results with code below

<div class="catagories-menu">
  <ul id="menu-content2" class="menu-content collapse show">
    <?
     $database = new DB();
     $query = "select * from category where cat_SubCat IS NULL";//
     $rows = $database->get_results($query);
     foreach ($rows as $row)//foreach database result
     {                              
        echo"<li data-toggle='collapse' data-target='#$row[cat_Name]' class='collapsed' >
                <a href='#' id='$row[cat_ID]'>" . $row[cat_Name] . "</a>
                <ul class='sub-menu collapse' id='$row[cat_Name]'>
                    <li><a href='#'>All</a></li>";
                    $database = new DB();//creating an object from the class
                    $query2 = "select * from category where cat_SubCat=" .$row[cat_ID]."";//
                    $subs = $database->get_results($query2);
                    foreach ($subs as $sub)//foreach database result
                    {
                      echo"<li><a href='prodDetail.php?cat_ID=".$sub[cat_ID]."&cat_SubCat=".$sub[cat_SubCat]."' class='list-group-item'>" . $sub[cat_Name] . "</a></li>";
                    }
            echo"</ul>
             </li>";
     }
     ?>
  </ul>
</div>

我正在寻找要首先打印的空类别,并在下面打印与空相关的类别 desired reults
从Khan的条目中更新了代码,并将$ subs = $ database-> get_results($ query);

替换为

$ subs = $ database-> get_results($ query2);
它部分起作用。代码不允许对前两个类别进行下拉,但其他类别的显示却很混乱

1 个答案:

答案 0 :(得分:0)

您在子菜单中使用的是查询,而不是query2

foreach ($rows as $row)//foreach database result
     {                              
        echo"<li data-toggle='collapse' data-target='#$row[cat_Name]' class='collapsed' >
                <a href='#' id='$row[cat_ID]'>" . $row[cat_Name] . "</a>
                <ul class='sub-menu collapse' id='$row[cat_Name]'>
                    <li><a href='#'>All</a></li>";
                    $database2 = new DB();//creating an object from the class
                    $query2 = "select * from category where cat_SubCat=" .$row[cat_ID]."";//
                    $subs = $database2->get_results($query2);
                    foreach ($subs as $sub)//foreach database result
                    {
                      echo"<li><a href='prodDetail.php?cat_ID=".$sub[cat_ID]."&cat_SubCat=".$sub[cat_SubCat]."' class='list-group-item'>" . $sub[cat_Name] . "</a></li>";
                    }
            echo"</ul>
             </li>";
     }