0

I'm passing the User_name with reference to that I have to fetch the visit_id which is in the third table, since my method is little messy can anyone tell how to achieve this using Codeigniter sub-query?

  public function fetch_patient_bill($user_name) {

    $this->db->select('id');
    $this->db->from('user');
    $query = $this->db->where('user_name', $user_name);
    $result = $query->get();
    foreach ($result->result() as $user) {}
    $user_id = $user->id;

    $this->db->select('id');
    $this->db->from('patient_registration');
    $query = $this->db->where('user_id', $user_id);
    $result = $query->get();
    foreach ($result->result() as $registration) {}
    $registration_id = $registration->id;

    $this->db->select('id');
    $this->db->from('patient_visit');
    $query = $this->db->where('patient_id', $registration_id);
    $result = $query->get();
    foreach ($result->result() as $visit) {}
    $visit_id = $visit->id;   //i want to fetch this id
    }
Madhu
  • 2,643
  • 6
  • 19
  • 34

1 Answers1

0

I don't clearly understand your question, but you can see an example using sub-queries in CI following this link: How to use subqueries in active record with Codeigniter

There is also a similar post here on StackO: subquery in codeigniter active record

Hope this helps

Community
  • 1
  • 1
Barilee
  • 57
  • 6