2012-09-16 95 views
5

所以我有3個表格我想加入。Codeigniter:加入3個表格並在視圖中顯示數據

我建立一個應用程序,我笨,我有3個表

客戶:
-id
-phone_number
-hospital_id
-smc_status
-testing_center_id

醫院
-id
-name

Testing_center
-id
-name

在模型中,我有這樣的:

public function get_clients() 
    { 
     if($slug === FALSE) 
     { 
      $this->db->select('clients.*'); 
      $this->db->from('clients'); 
      $this->db->join('hospital', 'clients.id = hospital.id'); 
      $this->db->join('testing_center', 'clients.id = testing_center.id'); 
      $query = $this->db->get(); 

      return $query->result_array(); 
     } 

     $query = $this->db->get_where('clients'); 
     return $query->row_array(); 
    } 

在視圖中我有:

<tbody> 
    <?php foreach ($clients as $client_item): ?> 
    <tr> 
     <td><?php echo $client_item['phone_number'] ?></td> 
     <td><?php echo $client_item['smc_status'] ?></td> 
     <td><?php echo $client_item['hospital_id'] ?></td> //i wish to have the hospital name here 
     <td><?php echo $client_item['testing_center_id'] ?></td> //i wish to have the testing center name here 
     <td><?php echo $client_item['language'] ?></td> 
     <td><a href="#">View</a></td> 
    </tr> 
    <?php endforeach ?> 
</tbody> 

但那是因爲因爲我沒有在第三和第四臺顯示醫院名稱和檢測中心名稱。我怎麼去解決這個問題?我嘗試了一些技術,因爲某些原因似乎沒有工作。請指教

回答

0

,如果你試試這個,會發生什麼:

$this->db->join('hospital', 'hospital.id = clients.id'); 
$this->db->join('testing_center', 'testing_center.id = clients.id'); 

,而不是這樣的:

$this->db->join('hospital', 'clients.id = hospital.id'); 
$this->db->join('testing_center', 'clients.id = testing_center.id'); 

還要檢查

客戶* 小號 *和客戶端,如果它們是相同的無處不在

還改變爲Ne rd建議: $ this-> db-> select('clients。*'); 到:

$this->db->select('*'); 
+0

我已經首次提出所有必要的更改由書呆子的建議,以及你的,但我最終得到了HTTP錯誤500,這是爲什麼? – raybesiga

+0

已選中,我犯了一些拼寫錯誤。感謝您的建議! – raybesiga

+0

我正在嘗試填寫表單並將數據返回到數據庫到不同的表中。我會怎麼做呢?我是否首先將表單綁定到數據庫,以便它理解連接,或者我應該將輸入文本置於與數據庫中預期的類似的位置? – raybesiga

3

你只能從clients表中選擇的值。您需要選擇從其他表中的列以及

$this->db->select('clients.id, 
    clients.phone_number, 
    clients.smc_status, 
    clients.language, 
    hospital.name AS hospital_name, 
    testing_center.name AS testing_center_name'); 

然後你就可以通過

<?php echo $client_item['hospital_name'] ?> 
<?php echo $client_item['testing_center_name'] ?> 

編輯訪問它們:你也shouldn't use SELECT *,這clients.*在做什麼。更新我的代碼。

+0

我做了更改,但最終得到了HTTP錯誤500.爲什麼? – raybesiga

+0

有許多不同的事情可能會出錯。檢查你的PHP日誌。 –

+0

我在本地主機上運行它,我無法檢查任何日誌,但我只是更改爲: $ this-> db-> select('clients.id, clients.phone_number, clients.smc_status, clients.language , hospital.name AS hospital_name,\t \t \t \t testing_center.name AS testing_center_name'); $ this-> db-> from('clients'); $ this-> db-> join('hospital','hospital.id = clients.id'); $ this-> db-> join('testing_center','testing_center.id = clients.id'); $ query = $ this-> db-> get(); return $ query-> result_array(); } $ query = $ this-> db-> get_where('clients',array('slug'=> $ slug)); return $ query-> row_array(); } } – raybesiga

0

這sholud是這樣

$this->db->join('hospital', 'clients.hospital_id = hospital.id'); 
$this->db->join('testing_center', 'clients.testing_center_id = testing_center.id'); 
+0

在兩個表格中加入相同的列名稱是錯誤的,例如(「hospital_id in clients,id in hospital」)和(「testing_center_id in clients,id in testing_center」) – Gautam3164

相關問題