0

首先,我是 mysqli 的新手并准备语句,所以如果您看到任何错误,请告诉我。我有这个静态下拉菜单: 在此处输入图像描述

HTML 代码:

<ul class="menu sgray fade" id="menu">

<li><a href="#">Bike</a>
    <!-- start mega menu -->
    <div class="cols3">
        <div class="col1">
            <ol>
                <li><a href="#">bikes</a></li>
                <li><a href="#">wheels</a></li>
                <li><a href="#">helmets</a></li>
                <li><a href="#">components</a></li>
            </ol>
        </div>
        <div class="col1">
            <ol>
                <li><a href="#">pedals</a></li>
                <li><a href="#">GPS</a></li>
                <li><a href="#">pumps</a></li>
                <li><a href="#">bike storage</a></li>
            </ol>
        </div>
                <div class="col1">
            <ol>
                <li><a href="#">power meters</a></li>
                <li><a href="#">hydratation system</a></li>
                <li><a href="#">shoes</a></li>
                <li><a href="#">saddles</a></li>
            </ol>
        </div>
    </div>
    <!-- end mega menu -->
</li>

我想做一个动态下拉菜单。我设法用这个功能展示了$categoryName和:$SubCategoryName

function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category"; 
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
while($stmt->fetch()) {
echo'<li><a href="#">'.$categoryName.'</a>
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'"; 
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
while($Substmt->fetch()) {
echo'
<li><a href="#">'.$SubCategoryName.'</a></li>';
            }
 echo'
  </ol>
</div>
   <!-- end mega menu -->
      </li>';
                 }
                  }

唯一的问题是它返回相同的所有子类别 <div class="col1">

在此处输入图像描述

我想获得的是计算子类别,如果结果超过 4,则返回第二列和第三列中的其他项目。

更新***:感谢下面的答案,现在菜单看起来像这样:

在此处输入图像描述

谢谢!

4

2 回答 2

1

试试这个怎么样?

进一步解释

发生的事情是,对于获取的每个子类别,我都会增加一个计数器。如果该计数器达到 4,它将结束<UL>and<DIV>并创建一个代表新列的新列。

function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category"; 
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
while($stmt->fetch()) {
echo'<li><a href="#">'.$categoryName.'</a>
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'"; 
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
$count = 0;
while($Substmt->fetch()) {
echo'
<li><a href="#">'.$SubCategoryName.'</a></li>';
$count+=1;
if ($count == 4) {
    $count = 0;
    echo '</ol></div><div class="col1"><ol>';
}
            }
 echo'
  </ol>
</div>
   <!-- end mega menu -->
  </li>';
             }
              }

编辑:误解了 col1 的目的。它们都应该是 col1 并且现在应该可以工作。如果没有,请给我评论!

于 2013-07-19T17:08:23.150 回答
0

试试这个:

function showMenuCategory(){
    $db = db_connect();
    $query = "SELECT * FROM Category"; 
    $stmt = $db->prepare($query);
    $stmt->execute();
    $stmt->bind_result($id,$categoryName,$description,$pic,$active);
    echo '<div class="cols3">';
    while($stmt->fetch()) {
        echo'<li><a href="#">'.$categoryName.'</a>
        <!-- start mega menu -->
        <div class="col1">
        <ol>';
        $dba = db_connect();
        $Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'"; 
        $Substmt = $dba->prepare($Subquery);
        $Substmt->execute();
        $Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
        while($Substmt->fetch()) {
            echo'<li><a href="#">'.$SubCategoryName.'</a></li>';
        }
        echo'</ol>';
    }
    echo '</div><!-- end mega menu --></li>';
}
于 2013-07-19T17:15:13.920 回答