0

Can I toggle bootstrap pills when a variable is set on my url using php? example

if (isset($_GET['user'])){
#profile is active
 }
else {
#home is active
}
Jeffrey Nerona
  • 135
  • 1
  • 10

1 Answers1

1

Sure, you just have to add a class="active" depending on your conditions. I would do it like this:

<ul class="nav nav-pills">
    <?php
        if (isset($_GET['user'])) {
            $activeMenu = 'profile';
        }
        else {
            $activeMenu = 'home';
        }
    ?>
    <li role="presentation" <?php echo ($activeMenu == 'home' ? 'class="active"' : ''); ?>><a href="#">Home</a></li>
    <li role="presentation" <?php echo ($activeMenu == 'profile' ? 'class="active"' : ''); ?>><a href="#">Profile</a></li>
    <li role="presentation"><a href="#">Messages</a></li>
</ul>

So it sets the $activeMenu variable to the menu you want active. And then we check that variable in each menu item and add the "active" class depending on its value.

Note this doesn't activate other menus (Messages) so you would have to add another condition (like I have) if you have other pills that you want active.

Cave Johnson
  • 6,499
  • 5
  • 38
  • 57