php - 这个PHP函数可以改进吗?

标签 php javascript

下面是我正在为导航菜单编写的一些代码,如果您在某个页面上,它将向适当的选项卡添加一个“当前”CSS 类。

我很好奇在 PHP 中是否有更好的方法来执行此操作,因为完成如此简单的任务确实需要很多代码?我的页面还将加载一个 jquery 库,用 jquery 而不是 PHP 设置选项卡会更好吗?任何提示表示赞赏

<?PHP

active_header('page identifier goes here'); //ie; 'home' or 'users.online'

function active_header($page_name)
{

    // arrays for header menu selector
    $header_home = array('home' => true);
    $header_users = array(
        'users.online' => true,
        'users.online.male' => true, 
        'users.online.female' => true, 
        'users.online.friends' => true, 
        'users.location' => true, 
        'users.featured' => true, 
        'users.new' => true, 
        'users.browse' => true, 
        'users.search' => true, 
        'users.staff' => true
    );
    $header_forum = array('forum' => true);
    $header_more = array(
        'widgets' => true, 
        'news' => true, 
        'promote' => true, 
        'development' => true, 
        'bookmarks' => true, 
        'about' => true
    );
    $header_money = array(
        'account.money' => true, 
        'account.store' => true, 
        'account.lottery' => true, 
        'users.top.money' => true
    );
    $header_account = array('account' => true);
    $header_mail = array(
        'mail.inbox' => true, 
        'mail.sentbox' => true, 
        'mail.trash' => true, 
        'bulletins.post' => true, 
        'bulletins.my' => true, 
        'bulletins' => true
    );

    // set variables if there array value exist
    if (isset($header_home[$page_name])){
        $current_home = 'current';
    }else if (isset($header_users[$page_name])){
        $current_users = 'current';
    }else if (isset($header_forum[$page_name])){
        $current_forum = 'current';
    }else if (isset($header_more[$page_name])){
        $current_more = 'current';
    }else if (isset($header_money[$page_name])){
        $current_money = 'current';
    }else if (isset($header_account[$page_name])){
        $current_account = 'current';
    }else if (isset($header_mail[$page_name])){
        $current_mail = 'current';
    }

    // show the links
    echo '<li class="' . (isset($current_home) ? $current_home : '') . '"><a href=""><em>Home</em></a></li>';
    echo '<li class="' . (isset($current_users) ? $current_users : '') . '"><a href=""><em>Users</em></a></li>';
    echo '<li class="' . (isset($current_forum) ? $current_forum : '') . '"><a href=""><em>Forum</em></a></li>';
    echo '<li class="' . (isset($current_more) ? $current_more : '') . '"><a href=""><em>More</em></a></li>';
    echo '<li class="' . (isset($current_money) ? $current_money : '') . '"><a href=""><em>Money</em></a></li>';
    echo '<li class="' . (isset($current_account) ? $current_account : '') . '"><a href=""><em>Account</em></a></li>';
    echo '<li class="' . (isset($current_mail) ? $current_mail : '') . '"><a href=""><em>Mail</em></a></li>';
}

?>

最佳答案

底部的两个非常大的代码块可以大大减少为一个简单的循环:

<?php

foreach (array('home', 'users', 'forum' /* ... */ ) as $item) {
  $ar = "header_$item";
  echo '<li class="', (isset($$ar[$page_name]) ? 'current' : '')
    , '"><a href=""><em>', ucword($item), '</em></a></li>';
}

?>

关于php - 这个PHP函数可以改进吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2097289/

相关文章:

php - 不支持 PDO 远程主机文件访问

javascript - 按下 Enter 键时也保持 Bootstrap-datetimepicker 始终打开

javascript - 如何使用带有类似变量的 React hooks?

Javascript 在 If 语句中错误地执行 MVC 代码

php - Magento Controller 适用于 Windows,不适用于 Linux

php - 使用 iOS 的社交网络登录

javascript - Symfony2 级联下拉值通过 ajax 更改,提交时不接受

php - 如何在 php htaccess 文件中的正则表达式中使用#

javascript - 是否可以将处理程序绑定(bind)到 jQuery 效果?

javascript - 如何使用 toggle classlist 更改 HTML 表格中一行的背景