2016-03-12 103 views
-2

對於CI我是新的,因爲MVC我認爲我的代碼不好。Codeigniter個人將數據從控制器傳遞到模型

我的控制器:

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風格的代碼?由於

+0

爲什麼不嘗試閱讀[文檔](http://www.codeigniter.com/user_guide/overview/mvc.html)和下面的[教程](http://www.codeigniter.com/user_guide/tutorial /index.html)首先? – Sparky

回答

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

在CONTROLER和傳遞,並通過$start_date$end_date到​​功能。 你將有你的代碼,因爲這:

控制器

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

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

你在你的控制器獲取表單信息,然後發送給模型進行一些計算或什麼都並返回給控制器,並把它傳遞給你的看法。

我希望它能幫助

相關問題