2015-09-04 66 views
2

这是我的导航菜单的布局。它工作得很好,因为它应该。但我想要PHP Navbar活动类

<li class="active"> 

在当前处于活动状态的页面上。我怎样才能做到这一点?

home.php:

<?php include 'includes/navbar.php'; ?>  

navbar.php:

<li><a href="?page=home">Home</a></li> 
<li><a href="?page=about">About</a></li> 
//etc 

的index.php:

$page = $_GET['page']; 
if (!isset($page)) { 
    include('home.php.php'); 
} 
if ($page == "home") { 
    include('home.php.php'); 
} 
if ($page == "about") { 
    include('about.php'); 
} 
//etc 
+0

index.php中的$ _GET是必要的吗?或者它是你的方法来实现你想要实现的目标 – Shehary

+1

请使用开关而不是ifelseif丛林 – Popnoodles

+0

'

  • >Home
  • ' – cske

    回答

    5

    你可以写一个if语句的每一个环节,但是这是一个更整洁的方法。

    navbar.php

    <?php 
    
    // using home as default, and not throwing a notice when $_GET['page'] isn't set 
    $page = (isset($_GET['page'])? $_GET['page'] : 'home'); 
    
    // create an array of pages and their titles 
    $pages = array(
        'home' => 'Home', 
        'about' => 'About', 
        // etc 
    ); 
    
    // output each, checking for which is active 
    foreach ($pages as $pagestring => $text){ 
        $active = ($pagestring == $page? ' class="active"' : ''); 
        echo '<li' . $active . '><a href="?page=' . $pagestring . '">' . $text . '</a></li>'; 
    } 
    
    ?> 
    

    如果某些页面出现的下拉列表(在问题未画出)一点点更多的工作需要做......这个NB包装整个事情与<ul>它没有按” t似乎在你的navbar.php文件中。

    $currentpage = (isset($_GET['page'])? $_GET['page'] : 'home'); 
    
    $pages = array(
        'home' => 'Home', // just use a string for a simple link 
        'about' => 'About', 
        'cute' => array(// use an array for a dropdown 
         'text' => 'Cute things', 
         'children' => array(
          'kittens' => 'Kittens', 
          'puppies' => 'Puppies', 
         ) 
        ), 
        // etc, you can include children in children too 
    ); 
    
    echo createLinksRecursive($pages, $currentpage); 
    
    function createLinksRecursive($array, $currentpage){ 
        $return = '<ul>'; 
        foreach ($array as $pagestring => $linkarray){ 
         // we don't want to worry about whether it's a string or array more than once 
         if (!is_array($linkarray)) $linkarray = array('text' => $linkarray); 
    
         // check for active 
         $active = ($pagestring == $currentpage? ' class="active"' : ''); 
    
         // add the li and anchor element 
         $return .= '<li' . $active . '> 
          <a href="?page=' . $pagestring . '">' . $linkarray['text'] . '</a>'; 
    
         // add children if there are any using the same function 
         if (isset($linkarray['children'])){ 
          $return .= createLinksRecursive($linkarray['children'], $currentpage); 
         } 
    
         // close that li 
         $return .= '</li>'; 
        }  
    
        // close the ul and return 
        $return .= '</ul>'; 
        return $return; 
    } 
    
    +0

    好吧,这是美好的,我感谢你帮助我!如果没有更多的麻烦;这种方法如何与下拉菜单一起工作? – Shanie93

    +0

    无论如何,我已经添加了 – Popnoodles

    +0

    我并不认为需要做更多的工作 - 但是,我非常感谢!非常感谢你的Popnoodles。 (: – Shanie93