Search code examples
codeigniter

How to get data from id using two table by passing an id


I have two join tables; parent and student. I have to update both tables from one button click. For that, I want to write a function "get data by id". I managed to write that code only for one table.

How do you write the following code if I want to get data from two tables? if p_id (parent id) is the foreign key?

Model

function get_by_id($id)
{
    $this->db->from('student');
    $this->db->where('p_id',$id);
    $query = $this->db->get();
    return $query->row();
}

Controller

public function ajax_edit($id)
{
    $data = $this->Model_Action->get_by_id($id);
    echo json_encode($data);
}

Solution

  •  function get_by_id($id)
    {
        $this->db->select('*')
        $this->db->from('student');
        $this->db->join('parent','student.p_id=parent.p_id');
        $this->db->where('student.p_id',$id);
        $query = $this->db->get();
        return $query->row();
    }