11

动态标题,CSS 类更改为 Active USING PHP(目录)

我希望在<li>活动目录下更改标签的类...现在,每个指南都向我展示了当您的页面等于它时如何执行此操作,但我想<li>根据我所在的目录更改

例如:

如果在
http://example.com/RESOURCES/code/opensource

http://example.com/RESOURCES/images/clipart上说我,
我希望“资源”^^<li>为“class="active"”,而其余显示 'class="noactive"'

或者如果我在
http://example.com/tutorials/css/flawless-dropdown-menu
我希望 "tutorials"<li>是 'class="active"' 而其余的是'class="noactive"'


网址设置:

这是我的网址显示方式的示例...

http://example.com/tutorials/css/flawless-dropdown-menu

^^那个 URL 是教程的页面....在“tutorials”目录下,而不是在“CSS”类别目录下,而不是页面标题(所有这些目录都不是真实的,是从 .htaccess 重写的)[无关]


导航设置:

<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>
4

11 回答 11

24

想出了答案……我想多了。

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 
$directoryURI = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURI, PHP_URL_PATH);
$components = explode('/', $path);
$first_part = $components[1];
?>
于 2012-11-12T03:33:59.277 回答
21

头文件.php

$activePage = basename($_SERVER['PHP_SELF'], ".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>
...
于 2015-06-30T18:47:43.147 回答
7

通过 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>
于 2012-11-11T23:18:07.423 回答
3

也许这可以帮助你:

$(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");
});
于 2012-11-11T23:08:57.377 回答
1

使用 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 />';
}
于 2012-11-11T23:11:28.390 回答
1

如果您使用 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 };?>
于 2018-02-14T07:33:36.263 回答
0

这是使用 PHP 的另一种方法:

<ul class="navbar-nav">
    <?php
        // Defines all pages in navigation
        $pages = array(
            'Home'     => 'index.php',
            'Products' => 'products.php',
            'Services' => 'services.php',
            'Contact'  => 'contact.php',
            'About'    => 'about.php'
        );

        // Gets active page URL
        $active = basename($_SERVER['PHP_SELF']);
        
        // Loops through all pages
        foreach ($pages as $title => $url) {
            // Checks if active url is matched and adds active CSS class
            if ($active === $url) {
                echo '<li><a href="'.$url.'" class="nav-link active">'.$title.'</a></li>';
            } 
            // Prints out default style for remaining links
            else {
                echo '<li><a href="'.$url.'" class="nav-link">'.$title.'</a></li>';
            }
        }
    ?>
</ul>
于 2022-02-21T16:20:34.880 回答
0

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

这在导航部分

<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>
于 2018-11-27T10:47:35.627 回答
0

在这里我们也可以做一个简单的事情:

<li class="page-scroll <?php if(basename($_SERVER['PHP_SELF'])=="aboutus.php"){echo "active";} ?>"><a href="aboutus.php">About us</a></li>
于 2020-01-09T07:39:53.510 回答
0
 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"); 
  ?> 
于 2019-06-14T07:30:36.207 回答
-1
<?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>
于 2014-10-10T07:02:58.533 回答