繁体   English   中英

这个PHP函数可以改进吗?

[英]Can this PHP function be improved?

下面是我正在处理导航菜单的一些代码,如果你在某个页面上,它会在正确的选项卡中添加一个“当前”的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>';
}

?>

你应该尽量不打印<li class="">或类似的东西; 它看起来很乱。 我已经移动了检查这个页面是否是要突出显示单独功能的页面,以防你最终更改$applicable_list的布局。

<?php
function active_header($page) {
    $applicable_list = array(
        "home" => array("home"),
        "users" => array(
            "users.online", "users.online.male", "users.online.female", "users.online.friends", 
            "users.location", "users.featured", "users.new", "users.browse", "users.search", "users.staff"
        ), 
        "forum" => array("forum"),
        "more" => array("widgets", "news", "promote", "development", "bookmarks", "about"),
        "money" => array("account.money", "account.store", "account.lottery", "users.top.money"),
        "account" => array("account"),
        "mail" => array("mail.inbox", "mail.sentbox", "mail.trash", "bulletins.post", "bulletins.my", "bulletins")
    );
    $pages = array_keys($applicable_list);

    function is_active_page($page, $category, $category_pages_list) {
        return array_key_exists($category, $category_pages_list) && in_array($page, $category_pages_list[$category]);
    }
    foreach($pages as $key => $category) {
        printf('<li%s><a href="#"><em>%s</em></a></li>' . "\n", 
            (is_active_page($page, $category, $applicable_list) ? ' class="current"' : ''),
            ucwords($category)
        );
    }
}

?>

您可以只使用页面名称数组,然后使用in_array ($page_name, $array)而不是isset($array[$page_name])进行比较,而不是将页面名称用作数组键。

这应该很好地与@meager的更改一起工作,并允许顶部的静态位代码缩小一点。

不妨投入我的命运。 输出限制为与原始问题中给出的匹配。

<?PHP

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

function active_header($page_name)
{
    // Unified array
    $headers = array(
        'Home' => array('home' => true),
        '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
        ),
        'Forum' => array('forum' => true),
        'More' => array(
            'widgets' => true, 
            'news' => true, 
            'promote' => true, 
            'development' => true, 
            'bookmarks' => true, 
            'about' => true
        ),
        'Money' => array(
            'account.money' => true, 
            'account.store' => true, 
            'account.lottery' => true, 
            'users.top.money' => true
        ),
        'Account' => array('account' => true),
        'Mail' => array(
            'mail.inbox' => true, 
            'mail.sentbox' => true, 
            'mail.trash' => true, 
            'bulletins.post' => true, 
            'bulletins.my' => true, 
            'bulletins' => true
        )
    );

    foreach($headers as $header => &$pages) {
        echo '<li class="';
        if(isset($pages[$page_name])) echo 'content';
        echo '"><a href=""><em>', $header, '</em></a></li>';
    }            

}

?>

我不是将代码与输出混合的粉丝,但它会做的例子。

PHP提示当天:如果你只是回显字符串,请不要使用字符串连接

合并您的数组,或将所有逻辑放在另一个命名良好的函数中。 我的例子合并了数组。

// it's ugly, but at least the ugliness
// is confined to only _one_ array ;)
$map_pages_to_navitem = array(
    'home' => 'home',
    'users.online' => 'users',
    'users.online.male' => 'users',
    'users.online.female' => 'users',
    'users.online.friends' => 'users',
    'users.location' => 'users',
    'users.featured' => 'users',
    'users.new' => 'users',
    'users.browse' => 'users',
    'users.search' => 'users',
    'users.staff' => 'users',
    'forum' => 'forum',
    'widgets' => 'more',
    'news' => 'more',
    'promote' => 'more',
    'development' => 'more',
    'bookmarks' => 'more',
    'about' => 'more',
    'account.money' => 'money',
    'account.store' => 'money',
    'account.lottery' => 'money',
    'users.top.money' => 'money',
    'account' => 'account'),
    'mail.inbox' => 'mail', 
    'mail.sentbox' => 'mail', 
    'mail.trash' => 'mail', 
    'bulletins.post' => 'mail', 
    'bulletins.my' => 'mail', 
    'bulletins' => 'mail', 
);
$current = $map_pages_to_navitem[$page_name];

echo '<li class="'.($current=='home')?'current':''.'"><a href=""><em>Home</em></a></li>';
echo '<li class="'.($current=='users')?'current':''.'"><a href=""><em>Users</em></a></li>';
echo '<li class="'.($current=='forum')?'current':''.'"><a href=""><em>Forum</em></a></li>';
echo '<li class="'.($current=='more')?'current':''.'"><a href=""><em>More</em></a></li>';
echo '<li class="'.($current=='money')?'current':''.'"><a href=""><em>Money</em></a></li>';
echo '<li class="'.($current=='account')?'current':''.'"><a href=""><em>Account</em></a></li>';
echo '<li class="'.($current=='mail')?'current':''.'"><a href=""><em>Mail</em></a></li>';

查看代码,我还看到最终结果是在<li>元素上赋予一个类属性值。 JavaScript比PHP更好。

因此,您可以为每个<li>一个id,并将class属性的赋值保留给JavaScript:

echo '<li id="home"><a href=""><em>Home</em></a></li>';
echo '<li id="users"><a href=""><em>Users</em></a></li>';
echo '<li id="forum"><a href=""><em>Forum</em></a></li>';
echo '<li id="more"><a href=""><em>More</em></a></li>';
echo '<li id="money"><a href=""><em>Money</em></a></li>';
echo '<li id="account"><a href=""><em>Account</em></a></li>';
echo '<li id="mail"><a href=""><em>Mail</em></a></li>';

echo '<script type="text/javascript">';
    echo 'document.getElementById("'.$current.'").className = "current";';
          // you'll want to sanitize $current to avoid parse errors in your JS
echo '</script>'

对于初学者使用switch而不是广泛的if/else :-)

    // 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';
}

你可以使用变量变量( http://www.php.net/manual/en/language.variables.variable.php),foreach和break来减少这部分功能

我没有看到任何令人信服的理由改变您的原始代码。 它易于阅读和理解,并且易于修改。 我也认为没有任何理由使用Javascript来设置标签指示器。 通过使用PHP,您可以满足禁用javascript的用户,并且我喜欢在真正需要时保存Javascript。

如果没有从“打印htmlcode”更改为“return htmlcode”,我至少会为此添加第二个参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM