CodeIgniter Forums
jone with three tables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: jone with three tables (/showthread.php?tid=34151)



jone with three tables - El Forum - 09-20-2010

[eluser]shahmy[/eluser]
Hi To All I have three tables in my database,
1.subject
2.grade
3.sub_to_grade.
I have to create a query to retrieve subject name belongs to grade id.this subject and grade contain many to many relationship.
I rote a query which i copy bellow. But it retrieve grade name belong to grade id.
If any one know please help me.
(Note: I am using xajax for retrieve)
Thank You.

Code:
$this->db->select("*");
              $this->db->from('subject');
              $this->db->join('grade_to_sub', 'grade_to_sub.sub_id=subject.sub_id');
              $this->db->join('grade', 'grade.grd_id=grade_to_sub.grd_id');
              $this->db->where(array('grade.grd_id'=>$grd_id));
            $query=$this->db->get();
            
            
        }
        $res = $query->result_array();
        if ($option=='html_option')
        {
            $list = '';
            for($x=0; $x<count($res); $x++)
            {
                $list .= "<option value='".$res[$x]['sub_id']."'>".$res[$x]['name']."</option>";
            }
            return $list;
        }
        else
        {
            return $res;
        }



jone with three tables - El Forum - 09-21-2010

[eluser]Georgi Budinov[/eluser]
Well this is because you have name field in both tables.
The fastest is to change $this->db->select("*"); to $this->db->select("*, subject.name");
I am not very aware with the ActiveRecord functionality as I am not using it. You can check how to set aliases for tables and please don't use * in the select - it is always better to name all the fields you want to retrieve.


jone with three tables - El Forum - 09-23-2010

[eluser]shahmy[/eluser]
Hi.. Georgi Budinov, Thank you very much.
It work now properly.