Codeigniter:加入3个表格并在视图中显示数据
所以我有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>
但那是因为因为我没有在第三和第四台显示医院名称和检测中心名称。我怎么去解决这个问题?我尝试了一些技术,因为某些原因似乎没有工作。请指教
,如果你试试这个,会发生什么:
$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('*');
你只能从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.*
在做什么。更新我的代码。
我做了更改,但最终得到了HTTP错误500.为什么? – raybesiga
有许多不同的事情可能会出错。检查你的PHP日志。 –
我在本地主机上运行它,我无法检查任何日志,但我只是更改为: $ 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
这sholud是这样
$this->db->join('hospital', 'clients.hospital_id = hospital.id');
$this->db->join('testing_center', 'clients.testing_center_id = testing_center.id');
在两个表格中加入相同的列名称是错误的,例如(“hospital_id in clients,id in hospital”)和(“testing_center_id in clients,id in testing_center”) – Gautam3164
我已经首次提出所有必要的更改由书呆子的建议,以及你的,但我最终得到了HTTP错误500,这是为什么? – raybesiga
已选中,我犯了一些拼写错误。感谢您的建议! – raybesiga
我正在尝试填写表单并将数据返回到数据库到不同的表中。我会怎么做呢?我是否首先将表单绑定到数据库,以便它理解连接,或者我应该将输入文本置于与数据库中预期的类似的位置? – raybesiga