Welcome Guest, Not a member yet? Register   Sign In
Query question and views
#1

[eluser]phybertek[/eluser]
Hello,

I'm having a hard time understanding how to query categories and subcategories from a database and passing this data from the model, to the controller and out to the view.

Table format:

Main
-----
id
name
value
date

Sub
----
id
mainid
name
value
desc
date

Code:
public function getvMain()
        {        
                
                 $query = $this->db->get_where('vmain',array('status' => 'active'));
                 return $query->result();
              
        }        
     public function getvCat($id)
        {
        
                $query = $this->db->get_where('vcat',array('mainid' => $id));
                return $query->result();
        }


I have a few methods in my model class. Method 1 selects * from main using active record class. and method 2 does the same for the sub. I'm trying to figure out how to combine this data as there is a One to Many relationship. Is there anyway to merge this data and sent it to my view?
#2

[eluser]toopay[/eluser]
Use join statement!
Code:
public function getCategory($id)
{
$query = $this->db->from('main_table')
                  ->join('sub_table', 'sub_table.mainid = main_table.id')
                  ->get();
return $query->result_array();
}




Theme © iAndrew 2016 - Forum software by © MyBB