How to add dynamic Active class on the selected page using PHP
I will explain how to dynamically add active class to navigation menu items in Bootstrap using popular server side language PHP. It will be quite simple code so it can be ported to other languages easily.
Example
<ul id="thicktabs">
<li class="<?php echo basename($_SERVER['PHP_SELF']) == 'index.php' ? 'active' : '';?>"><a href="index.php">Home</a></li>
<li class="<?php echo basename($_SERVER['PHP_SELF']) == 'manageUser.php' ? 'active' : '';?>"><a href="manageUser.php">Manage Users</a></li>
<li class="<?php echo basename($_SERVER['PHP_SELF']) == 'manageCompany.php' ? 'active' : '';?>"><a href="manageCompany.php">Manage Company</a></li>
<li class="<?php echo basename($_SERVER['PHP_SELF']) == 'manageSchedule.php' ? 'active' : '';?>"><a href="manageSchedule.php">Manage Schedule</a></li>
</ul>
Another Example
<ul id="thicktabs">
<li><a <?php echo basename($_SERVER['PHP_SELF']) == 'index.php' ? 'style="background:#dfeffe;color:#000000;"' : '';?> href="index.php">Home</a></li>
<li><a <?php echo basename($_SERVER['PHP_SELF']) == 'manageUser.php' ? 'style="background:#dfeffe;color:#000000;"' : '';?> href="manageUser.php">Manage Users</a></li>
<li><a <?php echo basename($_SERVER['PHP_SELF']) == 'manageCompany.php' ? 'style="background:#dfeffe;color:#000000;"' : '';?> href="manageCompany.php">Manage Company</a></li>
<li><a <?php echo basename($_SERVER['PHP_SELF']) == 'manageSchedule.php' ? 'style="background:#dfeffe;color:#000000;"' : '';?> href="manageSchedule.php">Manage Schedule</a></li>
</ul>
Leave a Reply