2011-12-24 51 views
0

我從所有文件中刪除了導航部分,並保存在一個單獨的文件名爲nav.php,然後把它稱爲在需要的地方作爲如何使導航項目處於選定狀態?

include ('includes/nav.php'); 

的nav.php文件的內容如下: :

<ul> 
     <li<?php if ($thisPage=="http://www.finalyearondesk.com") 
      echo " class=\"selected\""; ?>> 
       <a href="http://www.finalyearondesk.com">Home</a></li> 

     <li<?php if ($thisPage=="http://www.finalyearondesk.com/contact.php") 
      echo " class=\"selected\""; ?>> 
       <a href="http://www.finalyearondesk.com/contact.php">Contact US</a></li> 

     <li<?php if ($thisPage=="http://www.finalyearondesk.com/downloads.php") 
      echo " class=\"selected\""; ?>> 
       <a href="http://www.finalyearondesk.com/downloads.php">Downloads</a></li> 
</ul> 

但問題是,我無法獲得該頁面訪問時選擇的當前項目。 我的代碼有什麼問題?

+0

var $ thisPage等於什麼? – 2011-12-24 07:19:39

+0

我有三個頁面需要顯示。 – 2011-12-24 07:20:49

回答

0

你不分配價值$ thisPage。在nav.php中的ul前添加以下代碼

<?php 
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
       === FALSE ? 'http' : 'https'; 
$host  = $_SERVER['HTTP_HOST']; 
$script = $_SERVER['SCRIPT_NAME']; 

$thisPage = $protocol . '://' . $host . $script; 
?> 
+0

謝謝,它的工作,我以前不知道這一點。 – 2011-12-24 07:40:18

0

確保,要指定完整的URL $ thisPage這應該是你的前行

include ('includes/nav.php'); 

因爲在你niv.php你不分配$ thisPage