首先,我是 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,则返回第二列和第三列中的其他项目。
更新***:感谢下面的答案,现在菜单看起来像这样:
谢谢!