开发者

Same data variable passing to view

In my project, I have one search section with 3 select box. I passed the value to it using

$data开发者_JAVA技巧['restaurant_all']=$this->restaurant_model->get_all('','','','','','yes')->result();
$data['state_all']=$this->state_model->get_all();
$data['menu_all']=$this->menu_model->get_all('all','','','','','','yes')->result();
$data['restaurant']=$this->input->post('restaurant');
$data['state']=$this->input->post('area');
$data['food_type']=$this->input->post('menu');

I need this statement in all my pages. In there any way to accomplish this without writing these statements in all the pages


a. extend the default controller by creating a file MY_Contoller.php at a suitable location.

b. create a custom class that will extend the default controller.

c. add a protected or public variable $data to custom class.

e. do something with data using __construct()

d. make every controller extend the custom controller.

e. you can access this variable like any other class variable.

example code:

MY_Controller.php

class APP extends CI_controller {
        protected $data;

        function __construct() {
                parent::__construct();
                $this->_init();
        }

        function _init() {
                $this->data['state'] = $this->input->post('area');
        }
}

normal controllers:

class Welcome extends APP {

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

        function view() {
               /* pass this data value like normal data param */
               $this->load->view('some_view', $this->data);
        }
}

hope it helps.


Use constants, in /config/constants.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜