0

我使用这些文件作为我网站的基础:

menu.php: _

$p = $_GET['p'];

switch($p)
{
    case "home":
        $p = "page/home.php";
    break;

    case "customers":
        $p = "page/customers.php";
    break;

    default:
        $p = "page/home.php";
    break;
}

<ul id='nav'>

   <li>Title</li>
    <ul>
        <li class="nav active"><a href="?p=home">Home</a></li>
        <li><a href="?p=customers">Customers</a></li>
    </ul>
</ul>

default.php: _

<body>
 <div id='navcontainer'>
  <?php include ("menu.php"); ?>
 </div>

<div id='content'>
 <?php include ($p); ?>
</div>

style.css: _

#navcontainer {margin:0; padding:0; width:220px;float:left;}
#nav li {background:#36393D; color:#fff; padding:5px 3px 5px 20px; margin:4px;border-left:5px solid #3F4C6B;}
#nav ul li{display:block; padding:3px; background:#fff; color:#000;border:none; padding:3px 0 3px 30px;}
#nav ul li:hover, #nav ul li.active {background:#eee; cursor:pointer; border-left:5px solid #4096EE;padding:3px 0 3px 25px;}
#nav ul li a {text-decoration:none; color:#000;}

home.php所有页面:

<p>Hello world!</p>

我想知道是否有可能让我根据当前访问的页面menu.php切换到nav active项目。<li>

有任何想法吗?

4

3 回答 3

4

仅使用 php,您可以创建一个额外的变量

<?php
  $p = $_GET['p'];
  $currentPageId = 0;

  switch($p)
  {
    case "home":
        $p = "page/home.php";
        $currentPageId = 1;    
    break;

    case "customers":
        $p = "page/customers.php";
        $currentPageId = 2;    
    break;

    default:
        $p = "page/home.php";
        $currentPageId = 1;    
    break;
  }
?>

<ul id='nav'>

   <li>Title</li>
    <ul>
        <li <?php if($currentPageId == 1) { echo 'class="nav active"'; } ?> ><a href="?p=home">Home</a></li>
        <li <?php if($currentPageId == 2) { echo 'class="nav active"'; } ?> ><a href="?p=customers">Customers</a></li>
    </ul>
</ul>
于 2012-05-27T18:50:33.283 回答
3

您可以在 REQUEST 中提供一些 id,然后添加:

$id = $_GET['id']

然后

<li <?php if($id == your_id){ echo "class='nav visited'";} ?> >
于 2012-05-27T18:50:12.320 回答
0

我无法得到

$p = $_GET['p'];

去工作。对我有用的是

$p = $_SERVER[REQUEST_URI];
于 2014-04-21T15:13:57.873 回答