使用 PHP 将 class="active" 添加到活动页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13336200/
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
Add class="active" to active page using PHP
提问by Alex Sarnowski
Dynamic Header, CSS Class Change To Active USING PHP (dirrectory)
动态标题,CSS 类更改为 Active USING PHP(目录)
I want the class of the <li>tag to change under the active dirrectory...
now, every guide shows me how to do it when your page equals it, but i want to change
the <li>depending on what dirrectory im on
我希望<li>标签的类在活动目录下更改...现在,每个指南都向我展示了当您的页面等于它时如何做,但我想<li>根据我所在的目录来更改
for example:
例如:
if say im on
http://example.com/RESOURCES/code/opensource,
or
http://example.com/RESOURCES/images/clipart
i want the "RESOURCES" ^^ <li>to be 'class="active"' while the rest display 'class="noactive"'
or if im on
http://example.com/tutorials/css/flawless-dropdown-menu
I want the "tutorials" <li>to be 'class="active"' while the rest are 'class="noactive"'
如果我在
http://example.com/RESOURCES/code/opensource
或
http://example.com/RESOURCES/images/clipart上说,
我希望“RESOURCES”^^<li>为 'class="active"' 而其余显示 'class="noactive"'
或者如果我在
http://example.com/tutorials/css/flawless-dropdown-menu
我希望“教程”<li>是 'class="active"' 而其余的是'class="noactive"'
URL Setup:
网址设置:
This is my example of how my url's are displayed...
这是我的网址显示方式的示例...
http://example.com/tutorials/css/flawless-dropdown-menu
^^That URL is the page of a tutorial....under the "tutorials" directory, than under the "CSS" category directory, than the page title (all of these directories are not real and are rewrites from .htaccess) [irrelevant]
^^那个网址是教程的页面....在“tutorials”目录下,而不是在“CSS”类别目录下,而不是页面标题(所有这些目录都不是真实的,都是从.htaccess重写的)[无关]
Navigation Setup:
导航设置:
<ul id="mainnav">
<li class="noactive"><a href="/">Home</a></li>
<li class="active"><a href="/tutorials/">Tutorials</a></li>
<li class="noactive"><a href="/resources/">Resources</a></li>
<li class="noactive"><a href="/library/">Library</a></li>
<li class="noactive"><a href="/our-projects/">Our Projects</a></li>
<li class="noactive"><a href="/community/">Community</a></li>
</ul>
回答by Alex Sarnowski
Figured out the ANSWER...I was over thinking it.
找出答案...我想多了。
HTML
HTML
<ul id="mainnav">
<li class="<?php if ($first_part=="") {echo "active"; } else {echo "noactive";}?>"><a href="#">Home</a></li>
<li class="<?php if ($first_part=="tutorials") {echo "active"; } else {echo "noactive";}?>"><a href="#">Tutorials</a></li>
<li class="<?php if ($first_part=="resources") {echo "active"; } else {echo "noactive";}?>"><a href="#">Resources</a></li>
<li class="<?php if ($first_part=="library") {echo "active"; } else {echo "noactive";}?>"><a href="#">Library</a></li>
<li class="<?php if ($first_part=="our-projects") {echo "active"; } else {echo "noactive";}?>"><a href="#">Our Projects</a></li>
<li class="<?php if ($first_part=="community") {echo "active"; } else {echo "noactive";}?>"><a href="#">Community</a></li>
</ul>
PHP
PHP
<?php
$directoryURI = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURI, PHP_URL_PATH);
$components = explode('/', $path);
$first_part = $components[1];
?>
回答by vlycser
header.php
头文件
$activePage = basename($_SERVER['PHP_SELF'], ".php");
nav.php
导航.php
<ul>
<li class="<?= ($activePage == 'index') ? 'active':''; ?>"><a href="/index.php">Home</a></li>
<li class="<?= ($activePage == 'tutorials') ? 'active':''; ?>"><a href="/tutorials.php">Tutorials</a></li>
...
回答by Sean
Through PHP you can try -
通过 PHP 你可以尝试 -
<?php
// gets the current URI, remove the left / and then everything after the / on the right
$directory = explode('/',ltrim($_SERVER['REQUEST_URI'],'/'));
// loop through each directory, check against the known directories, and add class
$directories = array("index", "tutorials","resources","library","our-projects","community"); // set home as 'index', but can be changed based of the home uri
foreach ($directories as $folder){
$active[$folder] = ($directory[0] == $folder)? "active":"noactive";
}
?>
<ul>
<li class="<?php echo $active['index']?>"><a href="/">Home</a></li>
<li class="<?php echo $active['tutorials']?>"><a href="/tutorials/">Tutorials</a></li>
<li class="<?php echo $active['resources']?>"><a href="/resources/">Resources</a></li>
<li class="<?php echo $active['library']?>"><a href="/library/">Library</a></li>
<li class="<?php echo $active['our-projects']?>"><a href="/our-projects/">Our Projects</a></li>
<li class="<?php echo $active['community']?>"><a href="/community/">Community</a></li>
</ul>
回答by David Müller
Maybe this helps you:
也许这对你有帮助:
$(document).ready(function()
{
var parts = document.URL.split("/");
// [http:, empty, your domain, firstfolder]
var firstFolder = parts[3];
$("#mainnav li").attr("class", "noactive");
$("#mainnav a[href='/" + firstFolder + "/']").parent().attr("class", "active");
});
回答by Rick Calder
It's probably easier to do with jQuery but this works:
使用 jQuery 可能更容易,但这有效:
$url='http://example.com/tutorials/css/flawless-dropdown-menu';//pass the current url here instead of a static string.
$segments = explode ("/",$url);
$menuItems=array('Tutorials','Resources', 'Library', 'Our-Projects','Community');
$menu=array();
foreach ($menuItems as $menuItem) {
if($segments[3]==strtolower($menuItem)){
$menu[]=('<li class="active"><a href="/'.strtolower($menuItem).'/">'.str_replace("-"," ",$menuItem).'</a></li>');
} else {
$menu[]=('<li class="no-active"><a href="/'.strtolower($menuItem).'/">'.str_replace("-"," ",$menuItem).'</a></li>');
}
}
foreach ($menu as $item) {
echo $item.'<br />';
}
回答by kealaxshan
if you use mysql_fetch defined your row for menu title. if we take your menu title is MENU in mysql database and you have to put in (Home,tutorials,library,resources,our-projects,community)
如果您使用 mysql_fetch 为菜单标题定义了您的行。如果我们将您的菜单标题设为 mysql 数据库中的 MENU 并且您必须输入 (Home,tutorials,library,resources,our-projects,community)
<?php
//connect your data bass
include(connection.php');
//get your from ID like www.google?id=1
$id = $_GET['id'];
$query = "select * from pages where id='$id'";
$query1 = mysql_query($query);
while($row= mysql_fetch_array($query1))
{
?>
<html>
<?php $active= $row['MENU'];?>
<ul>
<li class="<?php if($active=='Home'){echo 'active';}else{echo'noactive';}?>"><a href="/">Home</a></li>
<li class="<?php if($active=='tutorials'){echo 'active';}else{echo'noactive';}?>"><a href="/tutorials/">Tutorials</a></li>
<li class="<?php if($active=='resources'){echo 'active';}else{echo'noactive';}?>"><a href="/resources/">Resources</a></li>
<li class="<?php if($active=='library'){echo 'active';}else{echo'noactive';}?>"><a href="/library/">Library</a></li>
<li class="<?php if($active=='our-projects'){echo 'active';}else{echo'noactive';}?>"><a href="/our-projects/">Our Projects</a></li>
<li class="<?php if($active=='community'){echo 'active';}else{echo'noactive';}?>"><a href="/community/">Community</a></li>
</ul>
</html>
<?php };?>
回答by Francisco Daniel Carvajal Bece
"includes/header.php" - This goes in Top of the File
“includes/header.php” - 这在文件的顶部
<?php
$activePage = basename($_SERVER['PHP_SELF']);
$index="";
$nosotros="";
$cursos="";
$contacto="";
switch ($activePage) {
case 'index.php':
$index=' class="current"';
break;
case 'nosotros.php':
$nosotros=' class="current"';
break;
case 'cursos.php':
$cursos=' class="current"';
break;
case 'contacto.php':
$contacto=' class="current"';
break;
default:
break;
}
?>
and this goes on the nav section
这在导航部分
<ul>
<?php
echo '<li'.$index.'><a href="/"><div>Inicio</div></a></li>';
echo '<li'.$nosotros.'><a href="nosotros"><div>Nosotros</div></a></li>';
echo '<li'.$cursos.'><a href="cursos"><div>Cursos</div></a></li>';
echo '<li><a href="http://www.redtechacademy.com"><div>Academia</div></a></li>';
echo '<li><a href="https://www.redtechhackingshop.com"><div>Tienda</div></a></li>';
echo '<li'.$contacto.'><a href="contacto"><div>Contacto</div></a></li>';
?>
</ul>
回答by Vijay Chauhan
Try the following:
<ul class="sub-menu">
<div class="h-10"></div>
<li class="<?php if ($your_variable=="test") {echo "active"; }
else{echo"noactive";}?>">
<a href="test.php">Test </a>
</li>
<li class="<?php if ($your_variable=="test2") {echo "active";
} else {echo"noactive";}?>">
<a href="test2.php" >Test2</a>
</li>
<li class="<?php if ($your_variable=="test3") {echo
"active"; } else {echo "noactive";}?>">
<a href="test3.php">Test3</a>
</li>
</ul>
**strong PHP text**
<?php
$directoryURI = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURI, PHP_URL_PATH);
$components = explode('/', $path);
$your_variable = basename($_SERVER['PHP_SELF'], ".php");
?>
回答by Samir Prajapati
Here we can do a simple thing also :
在这里我们也可以做一个简单的事情:
<li class="page-scroll <?php if(basename($_SERVER['PHP_SELF'])=="aboutus.php"){echo "active";} ?>"><a href="aboutus.php">About us</a></li>
回答by sachin
<?php $request_uri= $_SERVER['REQUEST_URI'];?>
<ul>
<li class="<?php if((strpos($request_uri,"index.html")!==false) || $request_uri=="" || $request_uri=="/"){echo "selected";}?>"><a href="/index.html">Home</a></li>
<li class="<?php if((strpos($request_uri,"service")!==false)){echo "selected";}?>"><a href="/services.html">Services</a> </li>
<li class="<?php if((strpos($request_uri,"product")!==false)){echo "selected";}?>"><a href="/products.html">Products</a></li>
<li class="<?php if((strpos($request_uri,"blog")!==false)){echo "selected";}?>"><a href="/blog.html">Blog</a></li>
<li class="<?php if((strpos($request_uri,"question")!==false)){echo "selected";}?>"><a href="/question-answers.html">Ques & Ans</a></li>
<li class="<?php if((strpos($request_uri,"career")!==false)){echo "selected";}?>"><a href="/career.html">Career</a></li>
<li class="<?php if((strpos($request_uri,"about-us")!==false)){echo "selected";}?>"><a href="/about-us.html">About</a></li>
</ul>

