dongsheng1698 2018-10-12 11:09
浏览 21

使用php无效的活动当前页面

I have this code in 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>

and on every page i put this code(and the name change depends on current page):

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

and in css i made this:

.active{color:red }

But it is not working... any idea why?

Thank you for time, I really appreciate it

  • 写回答

3条回答 默认 最新

  • douyunhuan9886 2018-10-12 11:12
    关注

    first you should remove $page = ""; from header

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

    Second in every page swap these two lines

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

    some details: when header load then $page is empty and your conditions false in li tag. so, before header load you should assign value to $page

    the best way: you should used $_SERVER['REQUEST_URI'] instead of hard coded $page. Example:

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

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图