Add class=“active” to active page using PHP

前端 未结 10 728
你的背包
你的背包 2020-12-01 06:47

Dynamic Header, CSS Class Change To Active USING PHP (dirrectory)

I want the class of the

  • tag to change under the active dirrectory... now
  • 相关标签:
    10条回答
    • 2020-12-01 07:38

      "includes/header.php" - This goes in Top of the File

      <?php 
      $activePage = basename($_SERVER['PHP_SELF']);
      
      $index="";
      $nosotros="";
      $cursos="";
      $contacto="";
      
      switch ($activePage) {
          case 'index.php':
              $index=' class="current"';
              break;
          case 'nosotros.php':
              $nosotros=' class="current"';
              break;
          case 'cursos.php':
              $cursos=' class="current"';
              break;
          case 'contacto.php':
              $contacto=' class="current"';
              break;
          default:
      
              break;
           }
      ?>
      

      and this goes on the nav section

      <ul>
          <?php 
              echo '<li'.$index.'><a href="/"><div>Inicio</div></a></li>';
              echo '<li'.$nosotros.'><a href="nosotros"><div>Nosotros</div></a></li>';
              echo '<li'.$cursos.'><a href="cursos"><div>Cursos</div></a></li>';
              echo '<li><a href="http://www.redtechacademy.com"><div>Academia</div></a></li>';
              echo '<li><a href="https://www.redtechhackingshop.com"><div>Tienda</div></a></li>';
              echo '<li'.$contacto.'><a href="contacto"><div>Contacto</div></a></li>';
          ?>
      </ul>
      
      0 讨论(0)
    • 2020-12-01 07:40

      It's probably easier to do with jQuery but this works:

      $url='http://example.com/tutorials/css/flawless-dropdown-menu';//pass the current url here instead of a static string.
      $segments = explode ("/",$url);
      
      $menuItems=array('Tutorials','Resources', 'Library', 'Our-Projects','Community');
      
      
      $menu=array();
      foreach ($menuItems as $menuItem) {
      if($segments[3]==strtolower($menuItem)){
          $menu[]=('<li class="active"><a href="/'.strtolower($menuItem).'/">'.str_replace("-"," ",$menuItem).'</a></li>');
      
      } else {
          $menu[]=('<li class="no-active"><a href="/'.strtolower($menuItem).'/">'.str_replace("-"," ",$menuItem).'</a></li>');
      }
      }
      foreach ($menu as $item) {
      echo $item.'<br />';
      }
      
      0 讨论(0)
    • 2020-12-01 07:42

      Here we can do a simple thing also :

      <li class="page-scroll <?php if(basename($_SERVER['PHP_SELF'])=="aboutus.php"){echo "active";} ?>"><a href="aboutus.php">About us</a></li>
      
      0 讨论(0)
    • 2020-12-01 07:50
       Try the following:
      
      <ul class="sub-menu"> 
         <div class="h-10"></div>
         <li class="<?php if ($your_variable=="test") {echo "active"; } 
           else{echo"noactive";}?>">
           <a href="test.php">Test </a>
         </li>
          <li class="<?php if ($your_variable=="test2") {echo "active"; 
            } else {echo"noactive";}?>">
           <a href="test2.php" >Test2</a>
          </li>
          <li class="<?php if ($your_variable=="test3") {echo 
           "active"; } else  {echo "noactive";}?>">
              <a href="test3.php">Test3</a>
          </li>
       </ul>
      
       **strong PHP text**
      
       <?php 
        $directoryURI = $_SERVER['REQUEST_URI'];
         $path = parse_url($directoryURI, PHP_URL_PATH);
         $components = explode('/', $path);
         $your_variable = basename($_SERVER['PHP_SELF'], ".php"); 
        ?> 
      
      0 讨论(0)
    提交回复
    热议问题