2013-05-07 60 views
0

我使用CodeIgniter活動記錄,並且缺乏在MySQL中連接多個表列的知識。加入兩個MySQL列並輸出colB.name,其中colA.id等於colB.id

我基本上想要做的,是輸出從ci_categories表中的所有行,但不是顯示ci_categories.component_id爲數字,我希望輸出的ci_components.name通過使用具有相等(或相同)ci_components.id從另一個表ci_components CodeIgniter中的活動記錄。

我已經做了,但有錯誤是:

型號:

public function getItemName($id){ 
    $this->db->select('*'); 
    $this->db->where('id', $id); 

    $result = $query->result(); 

    return $result; 
} 

和查看:

<?php echo $this->component_model->getItemName($cat['category']->com_id);?> 

任何提示請?

回答

1
$this->db->select('name') 
->from('ci_components') 
->where('ci_categories.component_id = ci_componenents.id'); 

$query = $this->db->get(); 

$this->db->select('name'); 
$this->db->from('ci_components'); 
$this->db->join('ci_categories', 'categories.component_id = ci_components.id'); 

$query = $this->db->get(); 

http://ellislab.com/codeigniter/user-guide/database/active_record.html#select

+0

而且在那裏我應該把這個代碼,在這種模式?進入'categories_model'或者'component_model'? – aspirinemaga 2013-05-07 15:36:01

+0

我可能會使用組件,因爲我們試圖檢索組件的名稱。 – stormdrain 2013-05-07 15:41:19