0
votes

I want to select data from 4 tables using Active Record class in codeIgniter

My tables: Teacher(id_teacher, name) Student(id_parent, name, id_teacher, id_class,id_school) class(id_class, libelle) School(id_school, name)

Here's my code :

$this->db->select("t.*, s.*, c.*, sh.*");
$this->db->from("teacher t");
$this->db->join("student s","t.id_teacher=s.id_teacher");
$this->db->join("class c","c.id_class=s.id_class");
$this->db->join("school sh","c.id_school=sh.id_school");

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

But it's not working. Can you help me please ?

1
You are not returning a result here are examples codeigniter.com/user_guide/database/results.htmlMr. ED
Yeah I do the result in "$query"... It's working with 3 tables but when I add the last table It don't workDev1996
missing double quote $this->db->from("teacher t); in this line...first put closing double quote mark in $this->db->from("teacher t); as finally $this->db->from("teacher t");webDev
Thanks but in my code i've done the double quote and it's not workingDev1996

1 Answers

0
votes

You had two from() I replaced the second from with join()

If you need a specific type of JOIN you can specify it via the third parameter of the function. Options are: left, right, outer, inner, left outer, and right outer.

public function test() {
    $this->db->select("t.*, s.*, c.*, sh.*");

    $this->db->from("teacher t");

    // $this->db->join("student s","t.id_teacher=s.id_teacher");

    $this->db->join('student s', 's.id_teacher = t.id_teacher' , 'LEFT');

    $this->db->join('class c', 'c.id_class = s.id_class', 'LEFT');

    // $this->db->from("school sh","c.id_school=sh.id_school");

    $this->db->join('school sh' ,"sh.id_school = c.id_school", 'LEFT');


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

    return $query->result_array(); 
}