2013-03-24 83 views
-1

我的菜单在我的一个目录中是一致的。例如,为包含的菜单创建if语句

首页 文档页 照片页

我希望能够有这样的菜单到每个网页,我会做。但是,这一次有点不同,因为即使目标是相同的,单个页面生成的链接也必须不同。就拿链接galleries.php页为例:

From the Home page:  <a href='galleries.php?id=<?=$id?>'> 
From the Documents page: <a href='galleries.php?id=<?=$doc[lid]?>'> 
From the Photos page:  <a href='galleries.php?id=<?=$photo[lid]?>'> 
From the Productss page: <a href='galleries.php??id=<?=$product[lid]?>'> 

>

我在做什么,现在是到菜单复制并粘贴到每个文件,并根据需要更改URL,但这不是一个非常令人满意的解决方案。我该如何在菜单中建立某种if语句,以便包含菜单的页面生成正确的链接?

回答

0

$_SERVER['REQUEST_URI']可以为您提供您当前所在的页面,并且您可以使用它来确定您应该链接到的位置。

例子:

function displayGalleryId($lid) { 
    $uri = $_SERVER['REQUEST_URI']; 
    switch ($uri) { 
     case '/home.php': 
      $link = $id; 
      break; 

     case '/documents.php': 
      $link = $doc[$lid]; 
      break; 

     // Others here... 

     default: 
      $link = 'gallery.php'; 
    } 

    return $link; 
} 

实例应用:

<a href='galleries.php?id=<? displayGalleryId($lid); ?> '> 
0

您需要使用$ _SERVER全局数组.. 可以使用$ _ SERVER [ 'SCRIPT_FILENAME']或$ _SERVER ['REQUEST_URI “]

它可像

function getLink($id) 
{ 
$uri    = $_SERVER['REQUEST_URI']; 
$is_page_home = (strstr($uri, 'home') === true)?true:false; 
$is_page_photos= (strstr($uri, 'photos') === true)?true:false; 
$is_page_document = (strstr($uri, 'document') === true)?true:false; 
if($is_page_home) 
{ 
    $urlid = $id 
} 
if($is_page_photos) 
{ 
    $urlid = $doc[$id] 
} 
if($is_page_document) 
{ 
    $urlid = $photo[$id] 
} 
return $urlId 
} 

$urlId = getLink($id) 

<a href='galleries.php??id=<?=$urlId?>'> 

了解更多关于服务器变量的信息http://php.net/manual/en/reserved.variables.server.php

+0

Thanks new_developer。这两种方法之间有什么区别,我在选择它们时应该考虑什么? – Janie 2013-03-24 13:27:32

+0

@Janie你问两个答案吗? – alwaysLearn 2013-03-24 13:29:09