如何使用CodeIgniter通过函数

时间:2018-01-22 08:36:45

标签: php codeigniter

我想将数据从Controller传递给model.But我无法在CI的模型端获取它。请帮助我。

这是我的控制器功能:

function show_chronicles($chronicle_num) {
    $this->load->database();

    //load the model  
    $this->load->model('Chronicles_model');
    //load the method of model  

    $data['h'] = $this->Chronicles_model->show_seminar();
    //return the data in view  
    $this->load->view('chronicles', $chronicle_num);
}

这是我的模型

public function show_seminar($chronicle_num) {
    echo $chronicle_num;
    //$this->db->select('*');
    //$this->db->where('chronicles_no',$chronicle_num);
    //$query1 = $this->db->get('chronicles');  
    //return $query1;  
}

3 个答案:

答案 0 :(得分:3)

因为你没有将任何价值传递给你的模特。

<强> CONTROLLER

function show_chronicles($chronicle_num)
{
    $this->load->database();

    $this->load->model('Chronicles_model');  

    $data['h']=$this->Chronicles_model->show_seminar($chronicle_num);  
    $this->load->view('chronicles', $chronicle_num);

}

,您需要返回查询的result()

<强> MODEL

public function show_seminar($chronicle_num = NULL)
      {  
         return $this->db
                     ->get_where('chronicles', array('chronicles_no' => $chronicle_num))
                     ->result();  
      } 

答案 1 :(得分:0)

<强>控制器

function show_chronicles($chronicle_num) {
    $this->load->database();

    //load the model  
    $this->load->model('Chronicles_model');
    //load the method of model  

    // pass it to model
    $data['chronicle_num'] = $this->Chronicles_model->show_seminar($chronicle_num);
    //return the data in view  
    $this->load->view('chronicles', $data);
}

这是我的模型

public function show_seminar($chronicle_num) {
    return $chronicle_num;
    //$this->db->select('*');
    //$this->db->where('chronicles_no',$chronicle_num);
    //$query1 = $this->db->get('chronicles');  
    //return $query1;  
}

答案 2 :(得分:0)

当您拨打$chronicle_num时,您忘了将$this->Chronicles_model->show_seminar();作为参数传递。

所以正确的方法是$this->Chronicles_model->show_seminar($chronicle_num);

您可以根据需要为函数添加任意数量的参数。例如:

public function show_seminar($chronicle_num, $param2, $param3) {
    //Login here
}

请记住每次调用函数时都要传递参数。