【问题标题】:Active current page using php not working使用 php 的活动当前页面不起作用
【发布时间】:2018-10-16 12:26:25
【问题描述】:

我在 header.php 中有这段代码:

<?php

 $page = "";
?>

<ul class="nav navbar-nav navbar-right nav-pos">
   <li <?php if ($page == "home") { ?> class="active"<?php } ?>><a href="home.php">Home</a></li>
   <li <?php if($page == 'about') { ?> class="active"<?php } ?>><a href="aboutus.php">About us</a></li>
   <li <?php if($page == 'contact'){ ?> class="active"<?php } ?>><a href="contactus.php">Contact us</a></li>
   <li class="hidden-xs"><a href="#search"><i class="fa fa-search fa-rotate-90"></i></a></li>
</ul>

在每一页上我都放了这段代码(名称更改取决于当前页面):

<?php include 'header.php';
  $page = 'about' ?>

在 css 中我做了这个:

.active{color:red }

但它不起作用......知道为什么吗?

谢谢你的时间,我真的很感激

【问题讨论】:

  • $page 始终仅在您输出该导航的位置包含空字符串,因为这是您分配给它的值。在您已经输出导航代码之后再更改值不会再更改任何内容,PHP 不是时间机器。
  • 你在哪里包含你的 header.php ?您能按原样向我们展示代码吗?
  • 无需设置和包含任何内容。只做这个:-&lt;?php $page = explode('.',end(explode('/',$_SERVER['REQUEST_URI']))[0]; ?&gt; &lt;ul class="nav navbar-nav navbar-right nav-pos"&gt; &lt;li &lt;?php if ($page == "home") { ?&gt; class="active"&lt;?php } ?&gt;&gt;&lt;a href="home.php"&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?php if($page == 'aboutus') { ?&gt; class="active"&lt;?php } ?&gt;&gt;&lt;a href="aboutus.php"&gt;About us&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?php if($page == 'contactus'){ ?&gt; class="active"&lt;?php } ?&gt;&gt;&lt;a href="contactus.php"&gt;Contact us&lt;/a&gt;&lt;/li&gt; &lt;li class="hidden-xs"&gt;&lt;a href="#search"&gt;&lt;i class="fa fa-search fa-rotate-90"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;
  • eddy abikhalil 你试过我的评论代码了吗?它有效吗?

标签: php css twitter-bootstrap


【解决方案1】:

问题在于,在您的 header.php 中,您明确设置了 $page = "" - 这意味着对 $page == 'about'(etc) 的任何检查都没有通过。

虽然您确实在主脚本中设置了$page = 'about',但这是在header.php 中的代码运行并生成其html 输出之后。

解决方法很简单:

1) 从header.php 中删除$page = ""

2) 设置$page = "about"(或等效项)包含

之前

【讨论】:

    【解决方案2】:

    您可以使用 jQuery 在页脚部分添加此脚本。

    $(document).ready(function() {
        // get current URL path and assign 'active' class
        var pathname = window.location.pathname;
        $('.nav > li > a[href="'+pathname+'"]').parent().addClass('active');
    });
    

    在你的情况下,这会起作用。

    $(function(){
        var current = location.pathname;
        $('.nav li a').each(function(){
            var $this = $(this);
            // if the current path is like this link, make it active
            if($this.attr('href').indexOf(current) !== -1){
                $this.addClass('active');
            }
        })
    });
    

    【讨论】:

      【解决方案3】:

      首先你应该从标题中删除$page = "";

      <?php
        $page = "";//remove this line in header file
      ?>
      
      <ul class="nav navbar-nav navbar-right nav-pos">
      

      在每一页第二次交换这两行

      <?php
         $page = 'about';//swap these lines
         include 'header.php' ;   
      ?>
      

      一些细节:当标头加载时,$page 为空并且li 标记中的条件为假。因此,在标题加载之前,您应该将值分配给 $page

      最好的方法:你应该使用$_SERVER['REQUEST_URI']而不是硬编码的$page。示例:

      $url= explode('/',$_SERVER['REQUEST_URI']);
      $page = end($request_array);//this show your_page.php
      $page=preg_replace('"\.php$"', '', $page )//this will remove .php extension
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-04
        • 1970-01-01
        相关资源
        最近更新 更多