Skip to content
Advertisement

Joining Query with codeigniter Model

Please help me how can I get only response where activities.activity_id = response.activity_id? here is my CI_model

public function get_response(){
   
    $this->db->select('*');
    $this->db->from('response');
     $id = $this->session->userdata('id');
   $this->db->where_in('response.user_id', $id);
   $this->db->join('activities', 'response.activity_id = activities.activity_id');
     $this->db->join('users', 'users.id = response.user_id');
     
    $result = $this->db->get()->result_array();
    return $result;
}

My Activities table

My Response Table

My users table

Advertisement

Answer

Try with this:

$id = $this->session->userdata('id');

$this->db->select('a.*, b.*, c.*');
$this->db->join('activities b', 'a.activity_id = b.activity_id');
$this->db->join('users c', 'c.id = a.user_id');
$this->db->where_in('a.user_id', $id);

$result = $this->db->get('response a')->result_array();

return $result;

You need to add aliases to the tables in order to build a more simplified and ordered query, in this case the aliases are a, b and c.

UPDATE: I fix a get method incorrectly writed when copy your code.

Check the query snippet shared here: https://extendsclass.com/mysql/526c246

I hope to be helpful

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement