Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can this PHP function be improved?

Tags:

javascript

php

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

?>
like image 367
JasonDavis Avatar asked Sep 28 '26 16:09

JasonDavis


1 Answers

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

?>
like image 66
meagar Avatar answered Sep 30 '26 06:09

meagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!