Welcome Guest, Not a member yet? Register   Sign In
jone with three tables
#1

[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;
        }
#2

[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.
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB