沃梦达 / 编程问答 / php问题 / 正文

如何正确构建突出当前页面的导航菜单

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;

这篇关于如何正确构建突出当前页面的导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何正确构建突出当前页面的导航菜单

基础教程推荐