2015-04-03 62 views
1

这里是我的如何使课堂活跃,只有对于那些开放

index.php 
blog.php 
contact.php 

我现在用的共同文件页面的页面。

这里是我的问题

我想说明的是打开我怎么能做到这一点

<a href='index.php' class='active'>Home</a> 
<a href='blog.php' class='active'>Blog</a> 
<a href='contact.php' class='active'>Contact</a> 

如果我展示class='active'的所有网页是不正确的class='active'只有网页,但怎么能证明它只是当前通过URL识别

它打开网页

我的网址会是这样的

www.mywebsite.com/index.php 
www.mywebsite.com/blog.php 
www.mywebsite.com/contact.php 

注:我没有使用任何框架,我只是用PHP核心

回答

1

您可以通过以下步骤

  1. 识别URL

    http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]做到这一点

  2. 用爆炸用于获取页面名称

  3. 使用substr

  4. ,并把它与条件

所以剩下的以延长即.php

$Url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
$Exploded = explode('/', $Url); 
$LastPart = end($Exploded); 
$ExactName = substr($LastPart, 0, -4); 

而且您将获得$ ExactName作为indexblogcontact

因此,从这里您可以根据需要制定条件以显示class='active'

条件:

我只是完全做到了为$Page

$Page = substr(end(explode('/', "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]")), 0, -4); 
$Class = 'class="active"'; 
<a href="index.php"<?php if($Page=='index' || $Page==''){ echo $Class; } ?> >Home</a></li> 
<a href="about.php" <?php if($Page=='about'){ echo $Class; } ?>>About</a> 
<a href="contact.php" <?php if($Page=='contact'){ echo $Class; } ?>>Contact</a></li> 
+0

感谢,我怎样才能使条件显示类=“主动”对每个项目,帮助PLZ – user4746941 2015-04-03 16:24:31

+0

你拯救了我的一天 – user4746941 2015-04-03 16:56:57