How to set current page “active” in php

后端 未结 17 1754
栀梦
栀梦 2020-12-02 21:38

Hi I have a menu on my site on each page, I want to put it in it\'s own menu.php file but i\'m not sure how to set the class=\"active\" for whatever page i\

相关标签:
17条回答
  • 2020-12-02 22:08

    I solved this using jQuery/javascript by running the code below each time my any page is loaded:

     $(document).ready(function () {    
        //Get CurrentUrl variable by combining origin with pathname, this ensures that any url appendings (e.g. ?RecordId=100) are removed from the URL
        var CurrentUrl = window.location.origin+window.location.pathname;
        //Check which menu item is 'active' and adjust apply 'active' class so the item gets highlighted in the menu
        //Loop over each <a> element of the NavMenu container
        $('#NavMenu a').each(function(Key,Value)
            {
                //Check if the current url
                if(Value['href'] === CurrentUrl)
                {
                    //We have a match, add the 'active' class to the parent item (li element).
                    $(Value).parent().addClass('active');
                }
            });
     });
    

    This implementation assumes your menu has the 'NavMenu' ID, and uses http://hostname/scriptname.php href attributes like so:

     <ul id="NavMenu">
       <li><a href="http://localhost/index.php">Home</a></li>
       <li><a href="http://localhost/smartphone.php">Smartphone</a></li>
       <li><a href="http://localhost/tablet.php">Tablet</a></li>
       <li><a href="http://localhost/about.php" class="active">About Us</a></li>
       <li><a href="http://localhost/contact.php">Contact Us</a></li>
    </ul>
    

    Read the javascript comments to see what's going on. If you prefer to use a different href layout (like in your original example), you have to play with the CurrentUrl variable a bit to get it to use the same layout as your href attributes.

    For me this was the easiest solution since I had an existing sites with a big menu and many pages, and wanted to avoid having to modify all pages. This allows me to throw in a piece javascript code in the header file (which was a central file already) which solves the problem for all existing pages.

    0 讨论(0)
  • 2020-12-02 22:09

    Simplere solution:

    Borrowing the code from asprin above;

    Create a new file menu.php where you will store the one and only copy of the menu. In this file, you will create a function addMenu($pageName) that take a parameter as the page name and returns a string consisting of the menu after having added the current tag.

    In your HTML code, you would include(menu.php) and then call the function addMenu with the current page name. So your code will look like this:

    menu.php

         <?php
    
            function addMenu($pageName){
                $menu = 
    
                    '<ul>
                        <li><a href="Öffnungszeiten.php"' . ($pageName == "Öffnungszeiten" ? "class=\"current\"" : "") . '><span>Öffnungszeiten</span></a></li>
                        <li><a href="sauna.php"' . ($pageName == "Öffnungszeiten" ? "class=\"current\"" : "") . '><span>Sauna</span></a></li>
                        <li><a href="frauensauna.php"' . ($pageName == "Frauensauna" ? "class=\"current\"" : "") . '><span>Frauensauna</span></a></li>
                        <li><a href="custom.php" ' . ($pageName == "lounge" ? "class=\"current\"" : "") . '><span>Beauty Lounge</span></a></li>
                        <li><a href="Feiertage.php"' . ($pageName == "feiertage" ? "class=\"current\"" : "") . '><span>Feiertage</span></a></li>                                                                                
                    </ul>';
              return $menu;
        }
            ?>
    

    And in your HTML, say this:

    <div id="menu">
                <?php
                    include('menu.php');
                    echo addMenu("index");
                    echo $hello;     
                ?>
            </div>
    
    0 讨论(0)
  • 2020-12-02 22:09

    This worked for me:

        function active_page($script){
    
        $actual = basename($_SERVER['PHP_SELF']);
    
      if($script == $actual){
          return 'active-page'; //class name in css
      }
    }
    
    0 讨论(0)
  • 2020-12-02 22:12

    It would be easier if you would build an array of pages in your script and passed it to the view file along with the currently active page:

    //index.php or controller
    
    $pages = array();
    $pages["offnungszeiten.php"] = "Öffnungszeiten";
    $pages["sauna.php"] = "Sauna";
    $pages["frauensauna.php"] = "Frauensauna";
    $pages["custom.php"] = "Beauty Lounge";
    $pages["feiertage.php"] = "Feiertage";
    
    $activePage = "offnungszeiten.php";
    
    
    //menu.php
    <?php foreach($pages as $url=>$title):?>
      <li>
           <a <?php if($url === $activePage):?>class="active"<?php endif;?> href="<?php echo $url;?>">
             <?php echo $title;?>
          </a>
      </li>
    
    <?php endforeach;?>
    

    With a templating engine like Smarty your menu.php would look even nicer:

    //menu.php
    {foreach $pages as $url=>$title}
       <li>
           <a {if $url === $activePage}class="active"{/if} href="{$url}">
             {$title}
          </a>
       </li>
    {/foreach}
    
    0 讨论(0)
  • 2020-12-02 22:12

    I have some simple example, see below:

    <?php
    function active($currect_page) {
        $url = $_SERVER['REQUEST_URI'];
    
        if($currect_page == $url){
            echo 'active';
        }
    }
    ?>
    
    <ul class="navbar-nav mr-auto">
      <li class="nav-item <?php active('/');?>">
        <a class="nav-link" href="/">Home</a>
      </li>
      <li class="nav-item <?php active('/other');?>">
        <a class="nav-link" href="/other">Other page</a>
      </li>
    </ul>
    
    0 讨论(0)
提交回复
热议问题