How add class=’active’ to html menu with php

why don’t you do it like this:

in the pages:

<html>
   <head></head>
   <body>
      <?php $page="one"; include('navigation.php'); ?>
   </body>
</html>

in the navigation.php

<div id="nav">
   <ul>
      <li>
          <a <?php echo ($page == 'one') ? "class="active"" : ""; ?> 
                 href="https://stackoverflow.com/questions/2913415/index1.php">Tab1</a>/</li>
      <li>
          <a <?php echo ($page == 'two') ? "class="active"" : ""; ?> 
                  href="index2.php">Tab2</a>/</li>
      <li>
          <a <?php echo ($page == 'three') ? "class="active"" : ""; ?> 
                  href="index3.php">Tab3</a>/</li>
   </ul>
</div>

You will actually be able to control where in the page you are putting the navigation and what parameters you are passing to it.

Later edit: fixed syntax error.

Leave a Comment