PHP 菜单活动 "Here" Link

PHP menu Active "Here" Link

我想将我的菜单放在一个单独的 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 -->

实现此目的的一种快速而肮脏的方法是在每个文件中放置以下内容:

<?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;
}

我找到了问题的解决方案,在样式文件中添加了这个 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>