Codeigniter将数据从控制器传递到模型

时间:2016-03-12 15:25:21

标签: php codeigniter model-view-controller

我是CI的新手,因为MVC我觉得我的代码不好。

我的控制器:

public function index(){
    $this->data->report = $this->order_m->get_report();
    parent::_view('report/list',$this->data);
}

我的模特

public function get_report(){

//somecode

if ($this->input->post('submit')){
   $start_date = $this->input->post('thn_start').'-'.$this->input->post('bln_start').'-01';
   $end_date = $this->input->post('thn_end').'-'.$this->input->post('bln_end').'-31';;
} else {
   $start_date = date('Y-m-d',strtotime("-62 day"));
   $end_date = date('Y-m-d');
}
$this->db->where('date_in BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
$this->db->order_by('id_order','desc');

//somecode

}

我如何为MVC风格制作此代码?感谢

2 个答案:

答案 0 :(得分:0)

控制器:

$data = array(
'start' => $this->input->post('thn_start'),
'end' => $this->input->post('thn_end')
);

$result = $this->your_model->get_report($data); // this sends data to model
$this->load->view('report/list', $result);

您可以在控制器中获取表单信息,然后将其发送给模型进行一些计算,或将其返回给控制器并将其传递给您的视图。

我希望它有所帮助

答案 1 :(得分:0)

我建议你把

//somecode

if ($this->input->post('submit')){
   $start_date = $this->input->post('thn_start').'-'.$this->input->post('bln_start').'-01';
   $end_date = $this->input->post('thn_end').'-'.$this->input->post('bln_end').'-31';;
} else {
   $start_date = date('Y-m-d',strtotime("-62 day"));
   $end_date = date('Y-m-d');
}

在控制器中传递并传递$start_date$end_dateget_report()函数。 您将拥有以下代码:

<强>控制器

public function index(){
    //somecode

    if ($this->input->post('submit')){
       $start_date = $this->input->post('thn_start').'-'.$this->input->post('bln_start').'-01';
       $end_date = $this->input->post('thn_end').'-'.$this->input->post('bln_end').'-31';;
    } else {
       $start_date = date('Y-m-d',strtotime("-62 day"));
       $end_date = date('Y-m-d');
    }
    $data['report'] = $this->order_m->get_report($start_date,$end_date);
    $this->load->view('report/list',$data,1);
}

<强>模型

public function get_report($start_date,$end_date){

//somecode
$this->db->where('date_in BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
$this->db->order_by('id_order','desc');

//somecode

}

为什么你没有使用load->view