如何在动态菜单中添加活动类。我在我的产品列表中使用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>
答案 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
}
?>