Below is some code I am working on for a navigation menu, if you are on a certain page, it will add a "current" css class to the proper tab.
I am curious if there is a better way to do this in PHP because it really seems like a lot of code to do such a simple task? My pages will also have a jquery library already loaded, would it be better to set the tab with jquery instead of PHP? Any tips appreciated
<?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 (isse开发者_开发知识库t($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>';
}
?>
The two very large blocks of code at the bottom could be reduced drastically to a simple loop:
<?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>';
}
?>
You should try not to print <li class="">
or something like that; it looks messy. I've moved the checking of whether this page or not is the one to highlight to a seperate function in case you end up changing the layout of $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)
);
}
}
?>
May as well throw in my lot. Output is restricted to match that given in the original question.
<?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>';
}
}
?>
I'm not a fan of mixing up the code with the output, but it'll do for example.
PHP hint of the day: Don't use string concatenation if you're just echoing a string
Rather than using the page names as array keys you could simply have arrays of the page names, and then compare using in_array($page_name, $array)
, rather than isset($array[$page_name])
.
This should happily work alongside the alterations from @meager, and would allow the static bits of code at the top to shrink a little.
Consolidate your arrays, or put all that logic in another well-named function. My example consolidates the arrays.
// 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>';
Looking at the code, I also see the end result is to assign on <li>
element a class attribute value. JavaScript will do this better than PHP.
So you could give each <li>
an id and leave the assignment of the class attribute to 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>'
Use a switch
instead of extensive if/else
for starters :-)
// 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';
}
You can use variables variable ( http://www.php.net/manual/en/language.variables.variable.php ), foreach and break to reduce this part of function
I don't see any compelling reason to change your original code. It's very easy to read and understand, and easy enough to modify. I also don't think there's any reason to use Javascript to set the tab indicator. By using PHP, you cater to people who have javascript disabled, and I like to save Javascript for when it's truly needed.
If not changing to "return htmlcode" from "printing htmlcode", i would at least add a second parameter for that.
精彩评论