简体   繁体   中英

How to send data from controller to model with CodeIgniter through functions

I want to pass data from Controller to model.But I am unable to fetch it at the model side in CI.Kindly help me out.

Here is my controller function:

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

And here is my model :

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

Its because your not passing any value to your model.

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

}

and you need to return the result() of your query

MODEL

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

Controller :

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

And here is my model :

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

You just forgot to pass $chronicle_num as a parameter when you called $this->Chronicles_model->show_seminar(); .

So the right way is $this->Chronicles_model->show_seminar($chronicle_num);

You can add as many parameters as you wish to the functions. Example:

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

Just remember to pass the parameters every time you call the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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