2015-03-19 111 views
-1

我正在尝试为我的网站编写侧边栏。对于这一点,我储存对对应于文件中的链接值,说names.txt中:PHP_SELF不等于

Home-index.php 
Coding-coding.php 

和我的PHP文件,然后将这些值并把它们变成对的侧连杆,即

<a href = "index.php">Home</a> 
<a href = "coding.php">Coding</a> 

等等。现在,我想让它对于我所在的页面而言,而不是显示超链接,它将显示文本的大胆版本。 即,如果我是的index.php,它输出

<strong>Home</strong> 
<a href = "coding.php">Coding</a> 

要做到这一点,我使用explode从文件分割我的琴弦,然后PHP_SELF检测是否是同一个页面。

最重要的是,由于脚本将在不同的页面,我把它放到一个文件(side.php)中,然后使用include('side.php')。在side.php里面有侧边栏功能,它将页面目录(通过PHP_SELF)作为参数。除了粗体文本外,其他所有工作都可以正常工作。帮帮我?

P.S.当我说PHP_SELF我的意思是$_SERVER['PHP_SELF']

谢谢!

(我的代码,或其一部分...)

在side.php:

<?php 
    function sidebar($input){ 
     # echo ($input); 
     $sitenav = fopen("file","r"); 
     $amts=0; 
     do{ 
      $sidebarvals[$amts]=fgets($sitenav); 
      $amts=$amts+1; 
     }while ($sidebarvals[$amts-1]!="EOF"); 
     $amts--; 
     for ($i=0;$i<$amts;$i++){ 
      $parts = explode("-",$sidebarvals[$i]); 
      if ("/" . $parts[1]!=$input){ 
       echo "<a href=" . $parts[1] .">" . $parts[0] . "</a>"; 
      }else{ 
       echo "<strong>" . $parts[0] . "</strong>"; 
      } 
      echo "<br>"; 
     } 
     fclose($sitenav); 
    } 
?> 

,并在每一页上:

include ('side.php'); sidebar($_SERVER['PHP_SELF']); 
+0

您应该发布您正在使用的代码。这个问题可能是'$ _SERVER ['PHP_SELF']'开始处的反斜线。 – jeroen 2015-03-19 09:21:00

+0

那么,我试图用“/”来连接字符串。 $ namefromfile – Thornkey 2015-03-19 09:23:28

+0

然后我会放我的代码。 – Thornkey 2015-03-19 09:23:37

回答

0

我所知PHP_SELF返回文件名完整的文件路径为index.php将返回/index.php。但看起来你正试图匹配index.php和返回的值/index.php。 因此,为了更正您必须从PHP_SELF获取文件名,并且可以通过这种方式来实现。

$url_array = explode('/',$_SERVER['PHP_SELF']); 
$last = count($url_array) - 1; 
$file = url_array[$last]; 
+0

谢谢。我试图通过在字符串的另一部分附加“/”来完成。为什么这不起作用? – Thornkey 2015-03-19 09:39:02