动态下拉菜单 - PHP

时间:2013-07-19 17:00:58

标签: php html mysqli

首先,我是mysqli的新手并准备好陈述,如果您发现任何错误,请告诉我。我有这个静态下拉菜单: enter image description here

HTML code:

<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">上的所有子类别:

enter image description here

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

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

enter image description here

谢谢!

2 个答案:

答案 0 :(得分:1)

试试这个怎么样?

进一步解释

正在发生的是,对于所提取的每个子类别,我都会增加一个计数器。如果该计数器达到4,它将结束<UL><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,现在应该工作。如果没有,请给我留言!

答案 1 :(得分: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>';
}