2015-04-03 18 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