Can this PHP function be improved?
Posted
by jasondavis
on Stack Overflow
See other posts from Stack Overflow
or by jasondavis
Published on 2010-01-19T21:35:46Z
Indexed on
2010/05/15
18:54 UTC
Read the original article
Hit count: 193
php
|JavaScript
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 (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>';
}
?>
© Stack Overflow or respective owner