Home » Php » php – Sidebar partials for multilevel user in Codeigniter

php – Sidebar partials for multilevel user in Codeigniter

Posted by: admin February 25, 2020 Leave a comment


what is the best code for sidebar partial for multilevel?

Is it just do in Controller like :

public function tambahNota()
    $head['title'] = 'Tambah Nota';

    if($this->session->userdata('userLevel') == 1){
    } elseif($this->session->userdata('userLevel') == 2){


.. or in View partials/sidebar?

<?php if ($this->session->userdata('userLevel') == 1) {?>
     ..script of admin sidebar..
<?php } else if($this->session->userdata('userLevel') == 2) {?>
     ..script of user sidebar..
<?php } ?>

Thank you.

How to&Answers:

Hi @Goldplate it is good to keep it at the view. Although both ways works, keeping it in the view makes it easy to change filenames when you need to.

You can create a helper function to keep things simple, for example:

  1. Create a helper with the name template_helper.php in the helpers directory.
  2. Add the code below.

    defined('BASEPATH') or exit('No direct script access allowed');
    function load_partial($view_path, $view_data = null)
        $ci =& get_instance(); //get_instance is an instance of codeigniter object
        if (strstr($view_path, '.')) {
            $view_path = str_replace('.', '/', $view_path);
        $ci->load->view($view_path, $view_data);

And you can use it like this:

<?php if ($this->session->userdata('userLevel') == 1) {?>
<?php } else if($this->session->userdata('userLevel') == 2) {?>
<?php } ?>

So here “sidebars” is a directory that contains files level_one.php and level_two.php

If you see a dot in ‘sidebars.level_one’ it is just replacing a forward slash ‘/’ and for easy access.

This is just to give you an idea. Let me know if it helps.


Both the process is good. It up to you to choose yours.

As it is templating, I like to keep it the view. so, later on, I can easily find the file for edit.