简体   繁体   English

如何在循环的边栏导航中停止活动类

[英]How to stop class of active in sidebar navigation in loop

I have coded a sidebar navigation and added loop in <li> for brand class. 我已经对侧边栏导航进行了编码,并在<li>为品牌类添加了循环。 which has extracted from database! 从数据库中提取的!

Below is the code of Sidebar Navigation. 以下是补充工具栏导航的代码。

Problem is <li class='active'> has enabled with all li. 问题是<li class='active'>已对所有li启用。 i only wanted to enable to active with opened page of site. 我只想启用网站打开页面的功能。 How to break down li using loop? 如何使用循环分解li?

<div class="col-sm-2">
    <nav class="nav-sidebar">
        <ul class="nav">
            <li><a href="<?php echo(BASE_URL); ?>index.php">Home</a></li>
            <?php
            try {

                $brands = Brand::get_brands();
                foreach ($brands as $b) {

                    echo("<li class='active'><a href='" . BASE_URL . "products/products.php?brandID=$b->brandID'>$b->brand_name</a></li>");
                }
            } catch (Exception $ex) {
                echo("<li>" . $ex->getMessage() . "</li>");
            }
            ?> 

            <li class="nav-divider"></li>
            <li><a href="<?php echo(BASE_URL); ?>login.php"><i class="glyphicon glyphicon-off"></i> Sign in</a></li>
        </ul>
    </nav>
</div>

Image of sidebar screenshot: Sidebar Navigation Screenshot 侧边栏屏幕截图的图像: 侧边栏导航屏幕截图

Based on the links you make, I assume that your URL looks something like yoursite.com/products/products.php?brandID=150 . 根据您建立的链接,我认为您的URL看起来像yoursite.com/products/products.php?brandID=150 You can use the brandID from the URL by calling $_GET['brandID'] . 您可以通过调用$_GET['brandID']从URL使用brandID。

So we check if it is set and compare the value to the brandID inside the loop. 因此,我们检查它是否已设置,并将该值与循环内的brandID进行比较。

<div class="col-sm-2">
    <nav class="nav-sidebar">
        <ul class="nav">
            <li><a href="<?php echo(BASE_URL); ?>index.php">Home</a></li>
            <?php
            try {

                $brands = Brand::get_brands();
                foreach ($brands as $b) {
                    // adding this line:
                    $class = ( isset($_GET['brandID']) and $_GET['brandID'] == $b->brandID) ? ' class="active"' : '';
                    echo("<li".$class."><a href='" . BASE_URL . "products/products.php?brandID=$b->brandID'>$b->brand_name</a></li>");
                }
            } catch (Exception $ex) {
                echo("<li>" . $ex->getMessage() . "</li>");
            }
            ?> 

            <li class="nav-divider"></li>
            <li><a href="<?php echo(BASE_URL); ?>login.php"><i class="glyphicon glyphicon-off"></i> Sign in</a></li>
        </ul>
    </nav>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM