【发布时间】:2015-09-17 10:18:34
【问题描述】:
短版: 使用 php 包含文件时,如何在导航菜单中突出显示当前页面?
完整版: 我有一个网站,我使用 navmenu.php 文件通过 php include 语句生成导航菜单。我需要在导航栏中用顶部和底部边框标记当前查看的页面。在继续使用 php 包含文件的同时执行此操作的最佳方法是什么?
起初我想如果使用 if/elseif 语句,但到目前为止我最好的想法是在 PHP 中创建一个 switch 语句来搜索 url,然后进行相应的调整。那可能吗?如果是这样,我该如何提取页面名称?例如,如果 url 是 www.domain.com/contact.php 我想拉 'contact'
<?php
$page = [file name];
switch ($page) {
case "services.php":
//Add the border to the services page <li>
break;
case "about.php":
//Add the border to the about us page <li>
break;
case "contact.php":
//Add the border to the contactpage <li>
break;
default:
//Add the border to the home page <li>
}
?>
基于this question,看来我应该可以使用
switch($_GET['????'])
提前感谢您的帮助!
添加: 这是php文件中的代码:
<?php
session_start();
echo "<nav class='nav-wrap'>
<ul class='navLeft'>
<li><a href='http://www.domain.com' title='Home'>HOME</a></li>
<li><a href='http://www.domain.com/about.php' title='About Us'>ABOUT US</a></li>
<li><a href='http://www.domain.com/portfolio.php' title='View our Portfolio'>PORTFOLIO</a></li>
</ul>
<ul class='navRight'>
<li><a href='http://www.domain.com/giving.php' title='Giving Back'>GIVING BACK</a></li>
<li><a href='http://www.domain.com/services.php' title='Services'>SERVICES</a></li>
<li><a href='http://www.domain.com/contact.php' title='Contact Us'>CONTACT US</a></li>
</ul>
</nav>";
?>
第二次编辑: 谢谢@anujnehra,那部分开始有意义了。我一直在尝试确定一旦开关识别出当前页面如何进行。我正在考虑使用隐藏的 css 类并将其更改为显示可能效果最好,但我不确定如何控制 PHP 开关中的 css。
这是我目前所拥有的:
CSS:
.currentPage {
border-bottom: 2px solid #ffffff;
border-top: 2px solid #ffffff;
display: none;
}
PHP 文件:
print_r(pathinfo("www.domain.com/contact.php"));
Array (
[dirname] => www.domain.com
[basename] => contact.php
[extension] => php
[filename] => contact
);
switch(filename){
case "about":
.currentPage#about {display:inline;}
break;
case "portfolio":
.currentPage#portfolio {display:inline;}
break;
case "giving":
.currentPage#giving {display:inline;}
break;
case "services":
.currentPage#services {display:inline;}
break;
case "contact":
.currentPage#contact {display:inline;}
break;
default:
.currentPage#home {display:inline;}
break;
}
echo "<nav class='nav-wrap'>
<ul class='navLeft'>
<li><a href='http://www.domain.com' title='Home' class='currentPage' id='home'>HOME</a></li>
<li><a href='http://www.domain.com/about.php' title='About Us' class='currentPage' id='about'>ABOUT US</a></li>
<li><a href='http://www.domain.com/portfolio.php' title='View our Portfolio' class='currentPage' id='portfolio'>PORTFOLIO</a></li>
</ul>
<ul class='navRight'>
<li><a href='http://www.domain.com/giving.php' title='Giving Back' class='currentPage' id='giving'>GIVING BACK</a></li>
<li><a href='http://www.domain.com/services.php' title='Services' class='currentPage' id='services'>SERVICES</a></li>
<li><a href='http://www.domain.com/contact.php' title='Contact Us' class='currentPage' id='contact'>CONTACT US</a></li>
</ul>
</nav>";
?>
【问题讨论】:
-
创建一个 $_SESSION 变量来存储当前页面,“include”文件将使用这个全局变量来知道要突出显示的选项。举个例子,我们需要更多的代码来理解你这样做的方式。
-
您可以使用 javascript 从 url 中获取页面名称,然后在同一脚本文件中使用 switch case 将菜单类设置为活动状态。
-
@ShaneSkinner,注意 anuj nehra 的回答,我认为这正是您所需要的。将“文件名”字段放入您的开关中。
标签: php url switch-statement element