2017-02-16 62 views
1

我想把我的菜单放在一个单独的PHP文件中,所以当我需要编辑它时,我只需要编辑它一次。当我想突出显示活动页面时,问题就开始了。有人可以帮我解决它吗?PHP菜单活动“这里”链接

<?php $currentPage = basename($_SERVER['SCRIPT_FILENAME']); ?> 

切换导航

     <li><a href="index.php" <?php if ($currentPage == 'index.php') { echo 'id="here"'; }?>><span class="glyphicon glyphicon-home"></span> Home</a></li> 
         <li><a href="about.php" <?php if ($currentPage == 'about.php') { echo 'id="here"'; }?>><span class="glyphicon glyphicon-picture"></span> About us</a></li> 
         <li class="dropdown"> 
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> <span class="glyphicon glyphicon-calendar"></span> Services <span class="caret"></span></a> 
          <ul class="dropdown-menu"> 
           <li><a href="services1.php" <?php if ($currentPage == 'services1.php') { echo 'id="here"'; }?>><span class="glyphicon glyphicon-user"></span> Drivers services</a></li> 
           <li><a href="services2.php" <?php if ($currentPage == 'services2.php') { echo 'id="here"'; }?>><span class="glyphicon glyphicon-wrench"></span> Shop services</a></li> 
          </ul> 
         </li> 

         <li><a href="application.php"<?php if ($currentPage == 'application.php') { echo 'id="here"'; }?>><span class="glyphicon glyphicon-pencil"></span> On-line Application</a></li> 
         <li><a href="contact.php" <?php if ($currentPage == 'contact.php') { echo 'id="here"'; }?>><span class="glyphicon glyphicon-envelope"></span> Contact us</a></li> 

        </ul> 

       </div><!-- /.navbar-collapse --> 
      </div><!-- /.container-fluid --> 
     </nav> 
     <div class="clearfix"> </div> 
    </div><!-- navbar --> 
+1

修复呢?你没有告诉我们这个问题 – nogad

+0

如果你把它放在一个单独的文件中会发生什么? 'basename($ _ SERVER ['SCRIPT_FILENAME'])'仍应该返回您正在查找的脚本名称。 – jrn

+0

@nogad当我悬停菜单时,它不会突出显示活动页面。 –

回答

0

我找到解决我的问题加入这个CSS样式中的文件:

/* The here ID identifies the current page and applies a white color to the text and a black background as a visual indicator. */ 
 

 
a#here { 
 
    background-color: #000 !important; 
 
    color: #fff !important; 
 
}

加上调用与下面的代码在每一页菜单:

<div> <?php 
       $file ='includes/menu.php'; 
       if (file_exists($file) && is_readable($file)) { 
       include($file); 
       } else { 
        throw new Exception("$file can't be found"); 
        } 
       include('includes/menu.php');?> 
    </div>  
-1

一个快速和肮脏的方法来实现这一目标是在每个文件把下面的:

<?php 
    include("header.php"); // Insert location of header file here 
?> 

,并在创建后您的header.php文件你的头块插入此

<?php $active= substr(basename($_SERVER['SCRIPT_FILENAME']),0, -4); ?> // Page Name 
<body <?php echo "class='$active'";?>> // This sets the bodies class to be the page name. 

终于在style.css中使用下面的代码来设置突出特点

// This is an example for my CSS. Insert your own css selector 

// In my css i have this which adds a small orange bar to the bottom of the navigation option. I set the opacity to 0 to not display it. 
.header nav ul li a:before 
{ 
    height: 5px; 
    background: #ea5c18; 
    opacity: 0; 
} 
// Then i set .PageName (the bodies class name) and set that specific nav element to have an opacity of 1. 
.PageName .header nav ul li:first-child a:before 
{ 
    opacity:1; 
} 
+0

脏是正确的,当你可以用$ _SERVER变量检测页面时, – nogad

+0

是的,忘了那个。编辑我的答案@nogad –

+0

多数民众赞成在OP已经有 – nogad