PHP如何将活动类添加到动态菜单?

时间:2017-06-15 17:25:36

标签: php class dynamic menu

如何在动态菜单中添加活动类。我在我的产品列表中使用while.But我无法理解如何添加到活动类。

<div class="col-md-4">
        <h3>Products</h3>
        <div class="list-group">


         <?php
         $db = new connection(); 
         $productsor = mysqli_query($db,"select * from products");

         while($productcek = mysqli_fetch_assoc($productsor)){ ?>

         <a href="products.php?products_id=<?php echo $productcek['products_id'];?>" class="list-group-item"> <?php echo $productcek['products_ad']; ?> </a>
         <?php
       }
       ?>
     </div>
   </div>

1 个答案:

答案 0 :(得分:1)

您需要检查先前设置的_GET参数并在while循环中进行比较

while($productcek = mysqli_fetch_assoc($productsor)){ ?>

    <?php $active = (isset($_GET['products_id']) && $productcek['products_id'] == $_GET['products_id']) ? 'active' : null; ?>

    <a href="products.php?products_id=<?php echo $productcek['products_id'];?>" class="list-group-item <?= $active; ?>"> <?php echo $productcek['products_ad']; ?> </a>

<?php
}
?>