简体   繁体   中英

How to load same content over all page?

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?

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM