Search code examples
codeigniter-3

Issue with add global variable codeigniter 3


I've admin dashboard with header available in all pages.

in Admin Controller I add function:

`class Admin_controller extends Admin_Core_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        
        $data['notification_count'] = $this->order_admin_model->get_all_notifications_count();
        $data['notification'] = $this->order_admin_model->get_all_notifications();


        $this->load->view('admin/includes/_header', $data);
        $this->load->view('admin/index');
        $this->load->view('admin/includes/_footer');
    }
}`

The problem is this working only for "home page (index)" dashboard. When I open anyother page then I get issue undefinied variable.

How can I call this variables in global?

`
        $data['notification_count'] = $this->order_admin_model->get_all_notifications_count();
        $data['notification'] = $this->order_admin_model->get_all_notifications();`

update:

I've one file Core_Controller.php and this file contains:

class Admin_Core_Controller extends Core_Controller

{

public function __construct()
{
    parent::__construct();

    if (!is_admin()) {
        redirect(admin_url() . 'login');
        exit();
    }

    //set control panel lang
    $this->control_panel_lang = $this->selected_lang;
    if (!empty($this->session->userdata('mds_control_panel_lang'))) {
        $this->control_panel_lang = $this->session->userdata('mds_control_panel_lang');
        //language translations
        $this->language_translations = $this->get_translation_array($this->control_panel_lang->id);
    }

    //check long cron
    if (check_cron_time_long() == true) {
        //delete old sessions
        $this->settings_model->delete_old_sessions();
        //add last update
        $this->db->where('id', 1)->update('general_settings', ['last_cron_update_long' => date('Y-m-d H:i:s')]);
    }
}

protected function render($view, $data = NULL)
{
    $data['notification_count'] = $this->order_admin_model->get_all_notifications_count();
    $data['notification'] = $this->order_admin_model->get_all_notifications();

    $this->load->view('admin/includes/_header', $data);
    $this->load->view($view, $data);
    $this->load->view('admin/includes/_footer');
}

public function paginate($url, $total_rows)
{
    //initialize pagination
    $page = $this->security->xss_clean($this->input->get('page'));
    $per_page = $this->input->get('show', true);
    $page = clean_number($page);
    if (empty($page) || $page <= 0) {
        $page = 0;
    }

    if ($page != 0) {
        $page = $page - 1;
    }

    if (empty($per_page)) {
        $per_page = 15;
    }
    $config['num_links'] = 4;
    $config['base_url'] = $url;
    $config['total_rows'] = $total_rows;
    $config['per_page'] = $per_page;
    $config['reuse_query_string'] = true;
    $this->pagination->initialize($config);

    return array('per_page' => $per_page, 'offset' => $page * $per_page);
}

}

You see I add your code here and now in Admin_Controller I add:

class Admin_controller extends Admin_Core_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $data['title'] = trans("admin_panel");

        $data['order_count'] = $this->order_admin_model->get_all_orders_count();
        $data['product_count'] = $this->product_admin_model->get_products_count();
        $data['pending_product_count'] = $this->product_admin_model->get_pending_products_count();
        $data['blog_posts_count'] = $this->blog_model->get_all_posts_count();
        $data['members_count'] = $this->auth_model->get_users_count_by_role('member');
        $data['latest_orders'] = $this->order_admin_model->get_orders_limited(15);
        $data['latest_pending_products'] = $this->product_admin_model->get_latest_pending_products(15);
        $data['latest_products'] = $this->product_admin_model->get_latest_products(15);
        
        
        

        $data['latest_reviews'] = $this->review_model->get_latest_reviews(15);
        $data['latest_comments'] = $this->comment_model->get_latest_comments(15);
        $data['latest_members'] = $this->auth_model->get_latest_members(6);
        $data['latest_transactions'] = $this->transaction_model->get_transactions_limited(15);
        $data['latest_promoted_transactions'] = $this->transaction_model->get_promoted_transactions_limited(15);

        $this->load->view('admin/includes/_header', $data);
        $this->render('admin/index');
        $this->load->view('admin/includes/_footer');
        
    }
    

and after this dashboard now working and everytime is refreshed every sec.


Solution

  • I would suggest creating a base controller with a render function, then have your controllers extend from this base controller and use this function to render their pages. The render function can then contain the variables that need to be available on all pages.

    Since you already have an Admin_Core_Controller class, you might be able to add the render function there instead (not sure of your project structure). Something like this:

    class Admin_Core_Controller // ...
    {
        // ...
    
        protected function render($view, $data = NULL)
        {
            $data['notification_count'] = $this->order_admin_model->get_all_notifications_count();
            $data['notification'] = $this->order_admin_model->get_all_notifications();
        
            $this->load->view('admin/includes/_header', $data);
            $this->load->view($view, $data);
            $this->load->view('admin/includes/_footer');
        }
    }
    

    Then use it to render your page in Admin_Controller:

    class Admin_controller extends Admin_Core_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function index()
        {
            $this->render('admin/index');
        }
    }
    

    Edit Your Admin_Controller class should look like this - I've removed the header and footer includes (those are already rendered by the render function) and passed the $data array to render:

    class Admin_controller extends Admin_Core_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function index()
        {
            $data['title'] = trans("admin_panel");
    
            $data['order_count'] = $this->order_admin_model->get_all_orders_count();
            $data['product_count'] = $this->product_admin_model->get_products_count();
            $data['pending_product_count'] = $this->product_admin_model->get_pending_products_count();
            $data['blog_posts_count'] = $this->blog_model->get_all_posts_count();
            $data['members_count'] = $this->auth_model->get_users_count_by_role('member');
            $data['latest_orders'] = $this->order_admin_model->get_orders_limited(15);
            $data['latest_pending_products'] = $this->product_admin_model->get_latest_pending_products(15);
            $data['latest_products'] = $this->product_admin_model->get_latest_products(15);
            
            
            
    
            $data['latest_reviews'] = $this->review_model->get_latest_reviews(15);
            $data['latest_comments'] = $this->comment_model->get_latest_comments(15);
            $data['latest_members'] = $this->auth_model->get_latest_members(6);
            $data['latest_transactions'] = $this->transaction_model->get_transactions_limited(15);
            $data['latest_promoted_transactions'] = $this->transaction_model->get_promoted_transactions_limited(15);
    
            $this->render('admin/index', $data);
        }
    }