简体   繁体   English

模型的codeigniter全局数组

[英]codeigniter global array for a model

In my controller I want to declare a global variable which will always fetch all areas from my db 在我的控制器中,我想声明一个全局变量,该变量将始终从数据库中获取所有区域

currently I'm passing $data['dropdowns'] in all methods in my class 目前,我正在类中的所有方法中传递$data['dropdowns']

{
$data['dropdowns']=loading some other model method
$this->load->view('commons/header',$data);  
}

{
  $data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);  
}

{
  $data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);  
}

{
   $data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);  
}

the thing is I want to now send $data['area'] to all the views without having to declare it again and again in each method 事情是我现在想将$data['area']发送到所有视图,而不必在每个方法中一次又一次地声明它

$data['area']= $this->area_model->get_all_locations();

You want to add global variable , but as per my suggest to use global function to use any where to using to send parameter, so please check below my code. 您想添加全局变量,但是按照我的建议使用全局函数在任何地方使用发送参数,因此请检查下面的代码。
Note : please load your model in application/config/autoload.php file 注意:请在application / config / autoload.php文件中加载模型

This is simple demo : controller 这是简单的演示:控制器

{
 $data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name');
 $this->load->view('commons/header',$data);  
}

{
 $data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name,user_name');
 $this->load->view('commons/header',$data);  
}

Your model 您的模特

function get_records($table_name,$field_name)
{
   $this->db->select("$field_name");
   $this->db->from("$table_name");
   $query=$this->db->get();

    return $query->result_array();

}

create a base_controller and placed in application/core 创建一个base_controller并放置在应用程序/核心中

class base_controller extends CI_Controller
{
    public $area = array();
    function __construct()
    {
        // Call the Controller constructor
        parent::__construct();
        $this->get_area();
    }
    public function get_area() {
        $this->load->model('area_model');
        $this->area= $this->area_model->get_all_locations();
    }
}

now $this->area is available in all controller which extends base_controller and all common functionality you can put here 现在$ this-> area在所有扩展base_controller的控制器中可用,并且可以在此处放置所有常用功能

class homepage extends base_controller{
    function __construct()
    {
        // Call the Controller constructor
        parent::__construct();

    }
public function index()
    {
        $data = $this->area; // call this wherever u need
        $this->load->view('commons/header',$data);

    }

}

importantly $this->area; 重要的是$ this-> area; one can use directly inside view 一个可以直接在内部视图中使用

Create a helper for your custom functions 为您的自定义功能创建一个助手

Eg: custom_helper.php then load your custom helper in autoload.php 例如: custom_helper.php然后将您的自定义帮助程序autoload.phpautoload.php

In custom_helper.php , create a function for getting area. custom_helper.php ,创建一个获取面积的函数。

if (!function_exists('get_area')) {
    function get_area() {
        $CI = & get_instance(); 
        $area= $CI->area_model->get_all_locations();
        return $area;
    }
}

You can call get_area() in your views without declaring in controllers.. 您可以在视图中调用get_area() ,而无需在控制器中声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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