How to properly build a navigation menu that highlights the current page(如何正确构建突出当前页面的导航菜单)
问题描述
我已经为一个基于 icant.co.uk的网站设置了一个菜单一个>.这很简单,可能只有 5 页.这个小站点主要是一个mysql浏览器,用于一些使用MATE的表.有一个 common.php 文件,其中包含标题 &页脚 HTML,所以我把代码放在下面.
I've setup a menu for a fairly simple site based on icant.co.uk. It's fairly simple with maybe 5 pages. The small site is mainly a mysql browser for a few tables using MATE. Theres a common.php file that contains the header & footer HTML so thats where I put the code below.
下面的代码突出显示菜单上的当前页面.它很难看,我相信一定有更好的方法来做到这一点.
The code below highlights the current page on the menu. Its ugly and I'm sure there has to be a better way to do it.
感谢您的帮助,谢谢!
这是我的代码
<?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>';
}
?>
更新 对于那些好奇的人,下面是工作代码!
UPDATE For those curious, below is the working code!
<?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;
?>
推荐答案
不确定这是否是你的意思,但这样你就可以摆脱这个丑陋的 if-else:
Not sure if that's what you meant, but this way you'll get rid of this ugly if-else:
$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" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '<a href="' . $page['file'] . '">' . $page['title'] .'</a>'
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
这篇关于如何正确构建突出当前页面的导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何正确构建突出当前页面的导航菜单
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01