我已经为一个基于icant.co.uk的相当简单的网站设置了一个菜单。这很简单,可能只有 5 页。小站点主要是使用MATE的几个表的 mysql 浏览器。有一个 common.php 文件,其中包含页眉和页脚 HTML,因此我将代码放在下面。
下面的代码突出显示菜单上的当前页面。它丑陋,我敢肯定必须有更好的方法来做到这一点。
任何帮助表示赞赏,谢谢!
这是我的代码
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
if ($currentFile == "orders.php"){
echo '<li id="active"><a href="orders.php" id="current">Orders</a></li>';
}
else{
echo '<li><a href="orders.php">Orders</a></li>';
}
if ($currentFile == "customers.php"){
echo '<li id="active"><a href="customers.php" id="current">Customer List</a></li>';
}
else{
echo '<li><a href="customers.php">Customer List</a></li>';
}
if ($currentFile == "order_details.php"){
echo '<li id="active"><a href="order_details.php" id="current">Order Details</a></li>';
}
else{
echo '<li><a href="order_details.php">Order Details</a></li>';
}
?>
更新对于那些好奇的人,下面是工作代码!
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
// easier to manage in case you want more pages later
$pages = array(
array("file" => "orders.php", "title" => "Orders"),
array("file" => "order_details.php", "title" => "Order Details"),
array("file" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$currentAppend = ($page['file'] == $currentFile) ? ' id="current' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '<a href="' . $page['file'] . '"' . $currentAppend . '">' . $page['title'] .'</a>'
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
?>