2013-02-18 61 views
0

Codeigniter不回數據庫結果。 數據庫表「類別」和「子類別」Codeigniter JOIN不回DB結果

DB瑪:

Categor 
----------------------------- 
ID  Name 
---------------------------- 
1  Fishing 
2  Hunting 
3  Test Category 

Sub_category 
----------------------------- 
ID  cat_id  name 
---------------------------- 
1  1   Fishing rod 
2  2   Hunting ammunition 
3  3   Test sub category 

我想列出所有子類的一些類別。當some1點擊釣魚類別我想要顯示所有子類別的釣魚。我的代碼是這樣的:

Controller: 
     public function get_sub_category($id = 0) 
     { 
      $this->load->model('front_m');  
      $data['sub_cat'] = $this->front_m->show_sub_cat($id);   
      $this->template->set_theme('zend')->set_layout('front.html') 
          ->build('sub_category',$data); 
    } 
    MODEL: 

    public function show_sub_cat($id=0) 
{ 
    $this->query = $this->db->select('*'); 
    $this->query = $this->db->from('category'); 
      $this->query = $this->db->where('id='.$id'); 
    $this->query = $this->db->join('sub_category', 'sub_category.cat_id=category.id'); 
    $this->query = $this->db->query('SELECT * FROM category'); 
    $this->query = $this->db->get(); 

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

什麼是我一直都有數據庫錯誤或毛坯頁。

回答

0

根據你的問題,這聽起來像你過度思考,但我也有點困惑。我聽到的是:如何基於某人點擊主類別ID來獲取我的子類別?如果是這樣的話,那麼你的方式就會比它所需要的更復雜。

模型

public function show_sub_cat($catid=NULL) 
{ 
$result = $this->db->get_where('sub_category',array('cat_id'=>$catid)); 
if ($result->num_rows()>0) 
{ 
return $result->result(); 
} 

} 
0
$sql_query ="SELECT 
       c.name, 
       sc.* 
      FROM Category as c 
      LEFT JOIN Sub_category as sc ON sc.cat_id = c.ID 
      WHERE c.ID = $id"; 
return $this->db->query($sql_query)->result();