Taslim A. Khan
Taslim A. Khan

Reputation: 526

how to pass variable from controller to model in PHP MVC

i have been trying to pass some variables to model through the controller function so that the variable can be used in corresponding query from database.Here is my code

public function chooseGroup()
{
    $data['area']=$_GET['area']; //variable from a view source;this is loaded successfully in this function, i have printed an echo to be sure.
    $this->load->model('information_model',$data);
    $groupdata['rows']= $this->information_model->getGroupDetails();
  // var_dump($groupdata);
}

model code:

function getGroupDeatils() {
            $this->db->select('area'); //area is suppose to contain the value 
            $q = $this->db->get('group'); //group is the table name
            if ($q->num_rows() > 0)
                foreach ($q->result() as $rows) {
                    $data[] = $rows;
                }
            return $data;
        }      

the value $data['area'] for some some reason isnt being recognized by the information_model and so the query is not processed.where did i go wrong?:( please help!

Upvotes: 0

Views: 2930

Answers (1)

Cody Covey
Cody Covey

Reputation: 1060

In your controller your call to the model should be like below

public function chooseGroup()
{
    $data['area']=$_GET['area']; 
    $this->load->model('information_model');
    $groupdata['rows']= $this->information_model->getGroupDetails($data);
}

And then in your Model declare the method like below

function getGroupDetail($data) {
    // query code here
}

Upvotes: 1

Related Questions