为包含的菜单创建if语句


Creating an if statement for an included menu

我有一个菜单在我的一个目录中是一致的。例如,

主页面文档页面照片页

我希望能够在每个页面中包含这样的菜单,这样我就完成了。但是,这次有点不同,因为即使目的地相同,各个页面生成的链接也必须不同。以指向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语句,以便包含菜单的页面生成正确的链接?

$_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); ?> '>

你需要使用$_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