尝试让我的PHP导航栏工作。如果页面是最新的,我试图回显用户所在的页面以及附加的活动类。
这是我的代码。它显示了链接,我可以浏览网站,但它忽略了活动类。悬停工程和链接的样式。它只是忽略了活动类。
<?php
$pages = array(
"index.php" => "Home",
"pages/contact.php" => "Contact Us",
"pages/services.php" => "services",
"pages/employees.php" => "Employees",
"pages/dashboard.php" => "Dashboard");
foreach ($pages as $url => $label) {
echo '<li ';
if (isset($_GET['page']) && $_GET['page'] == $url) {
echo '"class=active"';
}
echo '><a href=', "$url", '>', "$label", '</a></li>';
}
?>
答案 0 :(得分:2)
试试这个:
echo 'class="active"';
答案 1 :(得分:0)
这样做:
$pages = array(
"index.php" => "Home",
"pages/contact.php" => "Contact Us",
"pages/services.php" => "services",
"pages/employees.php" => "Employees",
"pages/dashboard.php" => "Dashboard");
foreach ($pages as $url => $label) {
echo '<li ';
if(basename($_SERVER['REQUEST_URI'])== basename($url)){ // this line
echo "class='active'"; // this line
} // and this line
echo '><a href=', "$url", '>', "$label", '</a></li>';
}
?>
答案 2 :(得分:0)
<?php
if ($_SERVER['REQUEST_URI'] === "/page") echo 'id="active"';
?>
答案 3 :(得分:0)
未经测试:
<?php
$pages = array(
"index.php" => "Home",
"contact.php" => "Contact Us",
"services.php" => "services",
"employees.php" => "Employees",
"dashboard.php" => "Dashboard");
$uri = explode("/", stripslashes($_SERVER['REQUEST_URI']));
foreach ($pages as $url => $label) {
echo '<li ';
echo in_array($url, $uri)?'class="active"':'';
echo '><a href="'.$url.'">'.$label.'</a></li>';
}
?>
注意:最好将其他页面文件放在你的index.php旁边的根目录中
答案 4 :(得分:0)
<?php
$pages = array(
"index.php" => "Home",
"pages/contact.php" => "Contact Us",
"pages/services.php" => "services",
"pages/employees.php" => "Employees",
"pages/dashboard.php" => "Dashboard");
$cururl = $_SERVER['PHP_SELF'];
foreach ($pages as $url => $label) {
echo '<li';
if (strpos($cururl,$url)!==false) {
echo ' class="active"';
}
echo '><a href=', "$url", '>', "$label", '</a></li>';
}
?>
这对你有用。因为,您尝试使用GET-METHOD
访问网址。在这里,我通过$cururl = $_SERVER['PHP_SELF'];
获取了您的网址并检查了活动菜单项if (strpos($cururl,$url)!==false)