O'Brien
O'Brien

Reputation: 43

Codeigniter: Two table query

I have two tables that I want to query. One table has records from the clinic while the other has records from the lab. This results in(Message: Trying to get property of non-object). What am I missing out?

I want to see the PID which are in clinic table but not in lab table.

My controller:

function queries(){
    $data['results'] = $this->main_model->queries();
    $this->load->view('queries', $data);

}

My Model:

function queries(){

    $this->db->select('*');
    $this->db->from('clinic');
    $this->db->where('participantID NOT IN (SELECT  participantID FROM lab)', NULL, FALSE);
    $results = $this->db->get();
    return $results;
}

Finally, my view:

<?php 

foreach($results as $row){


echo " <tr > <td> $row->participantID </td>  <td> </td> <td> </td> <td> </td> <td> </td> <td> </td></tr>";


} 

?>

Upvotes: 0

Views: 78

Answers (2)

CodeGodie
CodeGodie

Reputation: 12132

The problem lies in your model. You are not returning the proper array results. You have to first use get() to build the query and place it into a variable like $q, then use that variable with a the result() method to obtain the results, like this :

$q = $this->db->get(); 
return $q->result();

Upvotes: 0

Pranav Shah
Pranav Shah

Reputation: 99

Controller:

function getall(){      
$this->load->model('result_model');
$data['query'] =$this->result_model->result_getall();
print_r($data['query']);
die();
$this->load->view('result_view', $data);
}

Model:

function result_getall(){

$this->db->select('*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'left'); 
$query = $this->db->get();
return $query->result();

}

view:

 <?php foreach ($query as $row): ?>      

     <?php echo $row->answerA;?><br>

     <?php endforeach; ?>  

Upvotes: 1

Related Questions