active menu tabs with codeigniter

active menu tabs with codeigniter  using -'php,css,codeigniter'

Which method can be used in Codeigniter to achieve active menu tabs?

For example I have a user menu Profile Friends Messages Logout

When I'm in the profile controller the URL is The Profile tab should be active by changing the background color for example.

I would usually add php to the tabs to check for their corresponding URL segment and I would change the style of the active tab. In Codeigniter should I implement a helper? What function can I easily use in tabs?

asked Sep 15, 2015 by EdwSteere
0 votes

4 Answers

0 votes

As you said in your last paragraph, I would just check for the corresponding URI segment and change the style of the tab based on that.

Let's say you're using a foreach statement to generate your menu from a database:

    uri->segment(2) == url_title($menu_item->name, dash, TRUE)):?> class="active">>name;?>

The reason I threw url_title() in there is to make the name lowercase as the URI segment would be and take care of any possible spaces that may be in the menu name and/or URI segment. This would require the URL helper to be enabled. If they don't match, the default CSS for

  • elements would be used.

  • answered Sep 15, 2015 by JoniWBIItlb
    0 votes

    The tab styling is not a PHP but a CSS issue. Look up hover for color changes. They are often done with ul's, but not always. You can learn a lot here.

    As for list generation, a helper would probably be a good idea. You'll probably want the same code available for multiple different controls/methods and views, so having a generic get_menu(); or create_menu(); would be perfect in a helper file. You might want to add it to your MY_html_helper.php.

    (Future readers, please read comments)

    answered Sep 15, 2015 by HiltonMarkcv
    0 votes

    There is a simple example how to create controller based active menu.

       $ci =& get_instance();
       // get the current controller
       $controller = $ci->router->fetch_class(); 
       // you can generate this array dynamically
       $resources = array(0 => array("controller" => "home", "title" => "Home"),
                          1 => array("controller" => "products", "title" => "Products"),
                          2 => array("controller" => "services", "title" => "Services"),

    Of course, you can create more complex navigation menu based on both controllers and methods.

    answered Sep 15, 2015 by RenDiamond
    0 votes

    I had the same issue . Here is my solution:

    In each of the controllers, add a parameter in the function

    $data['activeTab'] == "profile";
    $this->load->view('viewname',$data); (Profile controller)
    $data['activeTab'] == "friends";
    $this->load->view('viewname',$data); (Friends controller)
    $data['activeTab'] == "messages";
    $this->load->view('viewname',$data); (Messages controller)

    In the view where you have the navigation bar,add a code something like this :


    This should solve the problem. Tell me if the problem persists.


    answered Sep 15, 2015 by Nqjdkicu