2013-03-05 79 views
0

我是codeigniter的新手。目前我正在做一個小項目作爲練習,我試圖在同一個網頁上顯示兩個表的數據。 我試圖在codeigniter中使用$this->db->join(); ,但它不適用於我。我想知道是否有其他選項可以探索在同一個網頁上顯示兩個表格數據?在同一網頁上顯示兩張表的數據

我也發佈我的連接方法,我試過了 - 也許你可以告訴我我做錯了什麼?

模式

$this->db->select('tblanswers.*,credentials.*'); 
    $this->db->from('tblanswers'); 
    $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'left'); 
    $query = $this->db->get(); 
    return $query->result(); 

與此連接功能的問題我有一個只顯示一個表格而不是其他。我使用print_r($data['query']); die()來檢查它只返回tblanswer不是兩個。

EDIT

表結構:

憑證

+--------------+------+-------------+-------+-------+ 
| cid(PRIMARY) | name | second_name | phone | email | 
+--------------+------+-------------+-------+-------+ 

tblanswers

+-------------------+--------+------------+---------+---------+---------+ 
| answerid(PRIMARY) | userid | questionid | answerA | answerB | answerC | 
+-------------------+--------+------------+---------+---------+---------+ 

回答

1

確定,所以首先你的表MUST有關係數據庫進行連接

return $this->db->select('tblanswers.*,credentials.*') 
         ->join('credentials cred', 'tblanswers.answerid = credentials.cid', 'LEFT') 
         ->get('tblanswers') 
         ->result_object() 

所以這會執行一個查詢從憑證獲取數據表,其中* answerid場= CID字段

EG

SELECT * FROM tblanswers 
    JOIN credentials ON credentials.cid = tblanswers.answerid 

編輯

好像你不活動需要使用一個連接你願意,你可以簡單地去

return $this->db->select('tblanswers.*,credentials.*') 
       ->from('tblanswers, credentials') 
       ->get() 
       ->result_object(); 

什麼,因爲它似乎像你有這兩者之間,但例如,你可以得到所有的任何關係數據通過去相關的問題

return $this->db->select('tblanswers.*,questions.*') 
         ->join('questions AS q', 'tblanswers.questionid = q.question_id', 'LEFT') 
         ->get('tblanswers') 
         ->result_object() 
+0

你能解釋更多關於關係數據嗎? TNN爲您的幫助 – 2013-03-05 08:44:31

+0

@EdvinasLiutvaitis確定編輯的問題,希望它解釋多一點點,但基本上它只能從證書表中選擇哪裏** tblanswers.answerid = credentials.cid **我也不知道你的佈局數據庫,所以我不舒服,如果你甚至需要這種類型的項目加入 – 2013-03-05 08:49:40

+0

它只會選擇在兩個表中具有相同名稱的列?我會編輯我的問題,並會提出我的表格結構 – 2013-03-05 08:51:42