0

In my controller I have called a model that makes a mysql query to fetch information from table. It is working okay but now under the same function I want to call another model and make a query based on one of the results from the previous mysql query. (The field name which I want to get the result of is "batch") . I have tried to get the value (batch) right in my controller, pass it into the model and then tried to make the second query but it seems like the second model is not getting the value from the controller and hence its not working. Would you please kindly help me with this? Thanks in Advance :)

Here is my Controller

        function Get($id){
                $this->load->model('mod_studentprofile');
                $data['query']= $this->mod_studentprofile->student_get($id);

                // To get the batch name
                $batch= $query ['batch']; // This I get from the above query result.

                $this->load->model('batchname');
                $data['query1']= $this->batchname->batchname($batch);

                $data['tab'] = "Student Profile";
                $data['main_content']='studentprofile';
                $this->load->view('includes/template',$data);

            }   

Here is my model number 1

 function student_get($id)
    {
        $query=$this->db->get_where('student',array('studentid'=>$id));


        return $query->row_array();

    }   

Here is my model number 2

   function batchname($batch)
    {
        $query1=$this->db->get_where('batch',array('batchid'=>$batch));

        return $query1->row_array();
    }   
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
black_belt
  • 6,601
  • 36
  • 121
  • 185

2 Answers2

0

Well, you're not actually assigning anything to $batch, how about this:

$batch= $data['query'];

This now passes the variable along. As a side note, you can pass it as a paramater and be done with it in a single line, dependency injection style:

$data['query1']= $this->batchname->batchname($this->mod_studentprofile->student_get($id));
Coccodrillo
  • 294
  • 3
  • 6
0

Are you getting a value back from the first query?

I would log the value you are getting back from the first query in your controller.

log_message('debug', 'Batch value is '.$batch);

And also put in some debug to see what the query that is being ran.

function batchname($batch)
{
        $query1=$this->db->get_where('batch',array('batchid'=>$batch));
        $str = $this->db->last_query();
        log_message('debug', 'Batchname Query: '.$str);
        return $query1->row_array();
}   
Bruce
  • 1,542
  • 13
  • 17