php 如何在php中设置当前页面“活动”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15963757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set current page "active" in php
提问by jenny
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'm on. Here is my code: please help me
嗨,我的网站上的每个页面都有一个菜单,我想将它放在自己的 menu.php 文件中,但我不确定如何为我所在的任何页面设置class="active"。这是我的代码:请帮助我
menu.php:
菜单.php:
<li class=" has-sub">
<a class="" href="javascript:;"><i class=" icon-time"></i> Zeiten<span class="arrow"></span></a>
<ul class="sub">
<li><a class="" href="offnungszeiten.php">?ffnungszeiten</a></li>
<li><a class="" href="sauna.php">Sauna</a></li>
<li><a class="" href="frauensauna.php">Frauensauna</a></li>
<li class=""><a class="" href="custom.php">Beauty Lounge</a></li>
<li><a class="" href="feiertage.php">Feiertage</a></li>
</ul>
</li>
采纳答案by package
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 Smartyyour menu.php would look even nicer:
使用像Smarty这样的模板引擎,您的 menu.php 看起来会更好:
//menu.php
{foreach $pages as $url=>$title}
<li>
<a {if $url === $activePage}class="active"{/if} href="{$url}">
{$title}
</a>
</li>
{/foreach}
回答by iraqi_love4ever
this method gets the current page using php which will pass a word in this case active and places it inside the class parameter to set the page active.
此方法使用 php 获取当前页面,在这种情况下将传递一个单词 active 并将其放置在 class 参数中以将页面设置为 active。
<?php
function active($currect_page){
$url_array = explode('/', $_SERVER['REQUEST_URI']) ;
$url = end($url_array);
if($currect_page == $url){
echo 'active'; //class name in css
}
}
?>
<ul>
<li><a class="<?php active('page1.php');?>" href="http://localhost/page1.php">page1</a></li>
<li><a class="<?php active('page2.php');?>" href="http://localhost/page2.php">page2</a></li>
<li><a class="<?php active('page3.php');?>" href="http://localhost/page3.php">page3</a></li>
<li><a class="<?php active('page4.php');?>" href="http://localhost/page4.php">page4</a></li>
</ul>
回答by chandresh_cool
Create a variable in each of your php file like :
在每个 php 文件中创建一个变量,例如:
$activePage = "sauna"; (different for each page)
then check that variable in your html page like this
然后像这样检查你的 html 页面中的变量
<?php if ($activePage =="sauna") {?>
class="active" <?php } ?>
回答by asprin
Put all the below code in menu.php
and everything will be taken care of.
把下面的所有代码menu.php
都放进去,一切都会得到照顾。
// function to get the current page name
function PageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$current_page = PageName();
Use the above to get the current page name then put this in your menu
使用上面的获取当前页面名称,然后将其放入您的菜单中
<li><a class="<?php echo $current_page == 'offnungszeiten.php' ? 'active':NULL ?>" href="offnungszeiten.php">?ffnungszeiten</a></li>
<li><a class="<?php echo $current_page == 'sauna.php' ? 'active':NULL ?>" href="sauna.php">Sauna</a></li>
<li><a class="<?php echo $current_page == 'frauensauna.php' ? 'active':NULL ?>" href="frauensauna.php">Frauensauna</a></li>
<li><a class="<?php echo $current_page == 'custom.php' ? 'active':NULL ?>" href="custom.php">Beauty Lounge</a></li>
<li><a class="<?php echo $current_page == 'feiertage.php' ? 'active':NULL ?>" href="feiertage.php">Feiertage</a></li>
where active
is the name of the class which will highlight your menu item
active
将突出显示您的菜单项的类的名称在哪里
回答by Netorica
there is two things you can do.
你可以做两件事。
first you can read the current filename of the php file you request by using $_SERVER['PHP_SELF']
or $_SERVER['REQUEST_URI']
or any other $_SERVER globalvariables that you can use to read your current page and compare it with the link's url, something like this
首先,您可以通过使用$_SERVER['PHP_SELF']
或$_SERVER['REQUEST_URI']
或任何其他$_SERVER 全局变量来读取您请求的 php 文件的当前文件名,您可以使用这些变量来读取当前页面并将其与链接的 url 进行比较,例如
<a href="offnungszeiten.php" <?php if($_SERVER['PHP_SELF']=='offnungszeiten.php'){ ?>class="activatepage" <?php } ?> >
?ffnungszeiten
</a>
the second one is to create a variable that you can read globally that would store the current name of the current page, like this
第二个是创建一个可以全局读取的变量,用于存储当前页面的当前名称,如下所示
<?php
$cur_page ="offnungszeiten"
?>
<a href="offnungszeiten.php" <?php if($cur_page=='offnungszeiten'){ ?>class="activatepage" <?php } ?> >
?ffnungszeiten
</a>
回答by Angela Taylor
I have done it with php in this way,
我是用这种方式用php完成的,
function createTopNav($active)
{
$pages = array(
array(
'name'=>'Home',
'link'=>'index'
),
array(
'name'=>'Smartphone',
'link'=>'smartphone'
),
array(
'name'=>'Tablet',
'link'=>'tablet'
),
array(
'name'=>'About Us',
'link'=>'about'
),
array(
'name'=>'Contact Us',
'link'=>'contact'
)
);
$res = "<ul>";
$activePage = "";
foreach($pages as $key=>$val)
{
if($val['link']==$active)
{
$res.= "<li><a href='".$val['link']."' class='active' >".$val['name']."</a></li>";
}
else
{
$res.= "<li><a href='".$val['link']."'>".$val['name']."</a></li>";
}
}
$res.="</ul>";
return $res;
}
And then to call this function
然后调用这个函数
echo createTopNav("about");
and the output will be like this
输出将是这样的
<ul>
<li><a href="index">Home</a></li>
<li><a href="smartphone">Smartphone</a></li>
<li><a href="tablet">Tablet</a></li>
<li><a href="about" class="active">About Us</a></li>
<li><a href="contact">Contact Us</a></li>
</ul>
回答by Alex
I solved this using jQuery/javascript by running the code below each time my any page is loaded:
我使用 jQuery/javascript 通过在每次加载任何页面时运行下面的代码来解决这个问题:
$(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:
此实现假设您的菜单具有“NavMenu” ID,并使用如下所示的http://hostname/scriptname.php
href 属性:
<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.
阅读 javascript 评论以了解发生了什么。如果您更喜欢使用不同的 href 布局(如在您的原始示例中),则必须稍微使用 CurrentUrl 变量才能使其使用与您的 href 属性相同的布局。
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.
对我来说,这是最简单的解决方案,因为我有一个带有大菜单和许多页面的现有站点,并且希望避免修改所有页面。这允许我在头文件(它已经是一个中心文件)中放入一段 javascript 代码,它解决了所有现有页面的问题。
回答by jimmy
A bit late on the ball, but I just had to solve this myself and ended up using this Javascript method, with a small modification. This has the advantage on not requiring many changes to the current code, just run the script and voila.
在球上有点晚了,但我只需要自己解决这个问题,最终使用了这个 Javascript 方法,做了一个小的修改。这样做的优点是不需要对当前代码进行很多更改,只需运行脚本即可。
window.onload = activateCurrentLink;
function activateCurrentLink(){
var a = document.getElementsByTagName("A");
for(var i=0;i<a.length;i++)
if(a[i].href == window.location.href.split("#")[0])
a[i].className = 'activelink';
}
回答by CodyBugstein
Simplere solution:
更简单的解决方案:
Borrowing the code from asprin
above;
借用asprin
上面的代码;
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.
创建一个新文件menu.php
,您将在其中存储菜单的唯一副本。在此文件中,您将创建一个函数addMenu($pageName)
,该函数将参数作为页面名称,并在添加当前标签后返回由菜单组成的字符串。
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:
在您的 HTML 代码中,您将使用当前页面名称include(menu.php)
调用该函数addMenu
。所以你的代码看起来像这样:
menu.php
菜单.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:
在你的 HTML 中,这样说:
<div id="menu">
<?php
include('menu.php');
echo addMenu("index");
echo $hello;
?>
</div>
回答by Ajeet Singh
Send page name in query string and check it on every page by getting the variable.
在查询字符串中发送页面名称,并通过获取变量在每个页面上进行检查。