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

时间:2009-10-09 21:22:35

标签: php css navigation uinavigationbar

我根据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;

&GT;

4 个答案:

答案 0 :(得分:3)

我通常做的是(对于所有元素......):

<li class="<?php if (condition) echo 'selected'; ?>">content part, links, etc.</li>

答案 1 :(得分:2)

不确定这是不是你的意思,但是这样你就可以摆脱这个丑陋的 - 如果不这样做:

$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;

答案 2 :(得分:2)

更简洁的方法(如果你启用了短标签)将是:

<li class="<?= $test=="your_page_name" ? 'selected' : 'not_selected'?>">Link Name</li>

它执行与第一个答案相同的功能,更简洁。

答案 3 :(得分:-1)

这是我的一个项目的片段。它是旧的丑陋代码,并使用表,但你可以很容易地使用div和清理标记的想法。诀窍是如果当前页面与其URL匹配,则导航使用不同的类。

    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_home.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_home.php'>Billing Home</a></td></tr>
    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_schedules.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_schedules.php'>Billing Schedules</a></td></tr>
    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_outstanding.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_outstanding.php'>Outstanding</a></td></tr>
    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_list.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_list.php'>List All</a></td></tr>
    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_history.php'>Billing History</a></td></tr>
    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_statement_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_statement_history.php'>Statement History</a></td></tr>