Add class=“active” to active page using PHP

前端 未结 10 726
你的背包
你的背包 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:26

      Maybe this helps you:

      $(document).ready(function()
      {
          var parts = document.URL.split("/");
          // [http:, empty, your domain, firstfolder]
          var firstFolder = parts[3];
      
          $("#mainnav li").attr("class", "noactive");
          $("#mainnav a[href='/" + firstFolder + "/']").parent().attr("class", "active");
      });
      
      0 讨论(0)
    • 2020-12-01 07:29
      <?php  $request_uri= $_SERVER['REQUEST_URI'];?>
      <ul>
       <li class="<?php if((strpos($request_uri,"index.html")!==false) || $request_uri=="" || $request_uri=="/"){echo "selected";}?>"><a href="/index.html">Home</a></li>
      
                      <li class="<?php if((strpos($request_uri,"service")!==false)){echo "selected";}?>"><a href="/services.html">Services</a> </li>
      <li class="<?php if((strpos($request_uri,"product")!==false)){echo "selected";}?>"><a href="/products.html">Products</a></li>
                      <li class="<?php if((strpos($request_uri,"blog")!==false)){echo "selected";}?>"><a href="/blog.html">Blog</a></li>
                      <li class="<?php if((strpos($request_uri,"question")!==false)){echo "selected";}?>"><a href="/question-answers.html">Ques & Ans</a></li>
                      <li class="<?php if((strpos($request_uri,"career")!==false)){echo "selected";}?>"><a href="/career.html">Career</a></li>
                      <li class="<?php if((strpos($request_uri,"about-us")!==false)){echo "selected";}?>"><a href="/about-us.html">About</a></li>
      
      </ul>
      
      0 讨论(0)
    • 2020-12-01 07:33

      if you use mysql_fetch defined your row for menu title. if we take your menu title is MENU in mysql database and you have to put in (Home,tutorials,library,resources,our-projects,community)

          <?php
      //connect your data bass
      
       include(connection.php');
      
      //get your from ID like www.google?id=1 
      
       $id = $_GET['id'];
          $query = "select * from pages where id='$id'";
          $query1 = mysql_query($query);
          while($row= mysql_fetch_array($query1))
          {
              ?>
      
      
      <html>
      <?php $active= $row['MENU'];?>
      <ul>
        <li class="<?php if($active=='Home'){echo 'active';}else{echo'noactive';}?>"><a href="/">Home</a></li>
        <li class="<?php if($active=='tutorials'){echo 'active';}else{echo'noactive';}?>"><a href="/tutorials/">Tutorials</a></li>
        <li class="<?php if($active=='resources'){echo 'active';}else{echo'noactive';}?>"><a href="/resources/">Resources</a></li>
        <li class="<?php if($active=='library'){echo 'active';}else{echo'noactive';}?>"><a href="/library/">Library</a></li>
        <li class="<?php if($active=='our-projects'){echo 'active';}else{echo'noactive';}?>"><a href="/our-projects/">Our Projects</a></li>
        <li class="<?php if($active=='community'){echo 'active';}else{echo'noactive';}?>"><a href="/community/">Community</a></li>
      </ul>
      </html>
      <?php };?>
      
      0 讨论(0)
    • 2020-12-01 07:35

      header.php

      $activePage = basename($_SERVER['PHP_SELF'], ".php");
      

      nav.php

      <ul>
          <li class="<?= ($activePage == 'index') ? 'active':''; ?>"><a href="/index.php">Home</a></li>
          <li class="<?= ($activePage == 'tutorials') ? 'active':''; ?>"><a href="/tutorials.php">Tutorials</a></li>
      ...
      
      0 讨论(0)
    • 2020-12-01 07:36

      Through PHP you can try -

      <?php 
      // gets the current URI, remove the left / and then everything after the / on the right
      $directory = explode('/',ltrim($_SERVER['REQUEST_URI'],'/'));
      
      // loop through each directory, check against the known directories, and add class   
      $directories = array("index", "tutorials","resources","library","our-projects","community"); // set home as 'index', but can be changed based of the home uri
      foreach ($directories as $folder){
      $active[$folder] = ($directory[0] == $folder)? "active":"noactive";
      }
      ?>
      
      <ul>
        <li class="<?php echo $active['index']?>"><a href="/">Home</a></li>
        <li class="<?php echo $active['tutorials']?>"><a href="/tutorials/">Tutorials</a></li>
        <li class="<?php echo $active['resources']?>"><a href="/resources/">Resources</a></li>
        <li class="<?php echo $active['library']?>"><a href="/library/">Library</a></li>
        <li class="<?php echo $active['our-projects']?>"><a href="/our-projects/">Our Projects</a></li>
        <li class="<?php echo $active['community']?>"><a href="/community/">Community</a></li>
      </ul>
      
      0 讨论(0)
    • 2020-12-01 07:37

      Figured out the ANSWER...I was over thinking it.

      HTML

      <ul id="mainnav">
          <li class="<?php if ($first_part=="") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Home</a></li>
          <li class="<?php if ($first_part=="tutorials") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Tutorials</a></li>
          <li class="<?php if ($first_part=="resources") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Resources</a></li>
          <li class="<?php if ($first_part=="library") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Library</a></li>
          <li class="<?php if ($first_part=="our-projects") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Our Projects</a></li>
          <li class="<?php if ($first_part=="community") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Community</a></li>
      </ul>
      

      PHP

      <?php 
      $directoryURI = $_SERVER['REQUEST_URI'];
      $path = parse_url($directoryURI, PHP_URL_PATH);
      $components = explode('/', $path);
      $first_part = $components[1];
      ?>
      
      0 讨论(0)
    提交回复
    热议问题