Derny
Derny

Reputation: 3

Join 3 tables with codeigniter and display it

I want to create a join database. in the 'relation' table will show 'disease' which can add many 'symptoms'. How should I do it?

Here is my database:

disease : id_disease, name, details
symptoms : id_symptoms, name, details
relation : id_relation, id_disease, id_symptoms

and here is my model

function get_allrelation() {
    $this->db->select('*');
    $this->db->from('relation');
    $this->db->join('disease','disease.id_disease = relastion.id_disease','left');
    $this->db->join('symptoms','symptoms.id_symptoms= relation.id_symptoms','left');

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

Please help me.

Upvotes: 0

Views: 157

Answers (2)

user4419336
user4419336

Reputation:

The controller below is a example only in the select $this->db->select('relation.*, disease.*, symptoms.*');

Oon the controller you can pass the data to the view the create a foreach loop in the view.

<?php

class Somecontroller extends CI_Controller {

    public function index() {

        $data['allrelation'] = $this->allrelation();


        $this->load->view('example', $data);

    }

    function get_allrelation() {
        $this->db->select('relation.*, disease.*, symptoms.*');
        $this->db->from('relation');
        $this->db->join('disease','disease.id_disease = relastion.id_disease','left');
        $this->db->join('symptoms','symptoms.id_symptoms = relation.id_symptoms','left');

        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result();
        }

        return false
    }

}

Upvotes: 0

Bojan Petkovic
Bojan Petkovic

Reputation: 2576

You had a typo: instead of relation you had relastion.id_disease.

Code below should work

function get_allrelation() {
  $this->db->select('*');
  $this->db->from('relation');
  $this->db->join('disease','disease.id_disease = relation.id_disease','left');
  $this->db->join('symptoms','symptoms.id_symptoms= relation.id_symptoms','left');

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

Upvotes: 1

Related Questions