gpt4 book ai didi

php - Codeigniter:连接 3 个表并在 View 中显示数据

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:42:03 25 4
gpt4 key购买 nike

所以我想加入 3 个表。

我正在用 Codeigniter 构建一个应用程序,我有 3 个表

客户:
-id
-电话号码
-医院编号
-smc_status
-testing_center_id

医院
-id
-姓名

测试中心
-id
-姓名

在模型中,我有这个:

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>

但那是因为我没有在第三和第四个td上显示医院名称和检测中心名称。我该怎么做?我尝试了一些技术,但由于某种原因似乎不起作用。请指教

最佳答案

您只是从 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.* 正在做。更新了我的代码。

关于php - Codeigniter:连接 3 个表并在 View 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12446972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com