2

Instead of calling same data inside each functions in controllers i want to load that data globally by calling once. i did

MY_Controller.php
<?php

class MY_Controller extends CI_Controller {

     public function __construct() {
         parent::__construct();
         $this->load->helper(array('form', 'url','file'));
         $this->load->model('common/common_model');
         $data['header_menus'] = $this->common_model->categoryMenus();
     }
}

This file is inside core folder and in controller folder there is a controller and i did

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends MY_Controller {

while loading function inside home controller how do i load $data['header_menus'] in views do i need to do saomething with that variable in function again?

Niroj Adhikary
  • 1,775
  • 18
  • 30

2 Answers2

1

Create one view in View folder and load that view in controller, make changes in your controller:

For example, your view name is display_view.php in view folder:

MY_Controller.php

<?php

class MY_Controller extends CI_Controller {

     public function __construct() {
         parent::__construct();
         $this->load->helper(array('form', 'url','file'));
         $this->load->model('common/common_model');
         $data['header_menus'] = $this->common_model->categoryMenus();
         $this->load->view('tour/view',$data);
     }
}

display_view.php

<?php
if(!empty($header_menus)) {
  extract($header_menus);
}
print_r($header_menus); // you can get all the info here
?>

Load View in Codeigniter

Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
0

Check about inheritance.

$data['header_menus'] = $this->common_model->categoryMenus();

has to be

$this->data['header_menus'] = $this->common_model->categoryMenus();

In extending controller you need to call it with

$this->data['header_menus'];

Use $this->data array for other variables too and call the view with something like:

$this->load->view('some_view_file', $this->data);

Read in docs here, here, here, here and here.

Tpojka
  • 6,996
  • 2
  • 29
  • 39