0

Codeigniterはデータベースの結果を返しません。データベーステーブル「Category」および「SubCategory」

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 ;       
}

私はいつもDBエラーまたは空白ページがあります。

4

2 に答える 2

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();
}

}
于 2013-02-18T16:56:00.957 に答える
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();
于 2013-02-18T19:03:01.200 に答える