这个PHP函数可以改进吗?

时间:2010-01-19 21:35:46

标签: 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>';
}

?>

9 个答案:

答案 0 :(得分:4)

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

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

?>

答案 1 :(得分:3)

你应该尽量不打印<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)
        );
    }
}

?>

答案 2 :(得分:2)

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

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

答案 3 :(得分:2)

也许还要投入我的命运。输出限制为与原始问题中给出的匹配。

<?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提示:如果你只是回显字符串,请不要使用字符串连接

答案 4 :(得分:1)

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

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

答案 5 :(得分:0)

首先使用switch代替广泛的if/else: - )

答案 6 :(得分:0)

    // 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来减少这部分功能

答案 7 :(得分:0)

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

答案 8 :(得分:0)

如果没有从“打印html代码”更改为“返回html代码”,我至少会为此添加第二个参数。

相关问题