Php 传递给视图的数据变量相同

Php 传递给视图的数据变量相同,php,codeigniter,Php,Codeigniter,在我的项目中,我有一个带有3个选择框的搜索部分。我使用 $data['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','','','','

在我的项目中,我有一个带有3个选择框的搜索部分。我使用

$data['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');

我需要在我所有的页面上都有这个声明。要实现这一点,无需在所有页面中写入这些语句,请在
/config/constants.php
a中使用常量。通过在适当的位置创建文件MY_Contoller.php来扩展默认控制器

b。创建将扩展默认控制器的自定义类

c。将受保护或公共变量$data添加到自定义类

e。使用_construct()对数据执行某些操作

d。使每个控制器扩展自定义控制器

e。您可以像访问任何其他类变量一样访问此变量

示例代码:

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');
        }
}
普通控制器:

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);
        }
}

希望有帮助。

我已经找到了这个解决方案。但是第一次它不起作用,因为我在application/libraries文件夹中创建了这个文件。但当我把它改为应用程序/核心时,它对我来说很有用。不过,谢谢你的回答,很高兴这对你有用。此外,请确保在使用CI 2.x时;扩展应用程序/核心文件夹中的所有核心类