Codeigniter:加入3个表并在视图中显示数据

ray*_*iga 5 php codeigniter

所以我想加入3张桌子.

我正在构建一个app i Codeigniter,我有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();
    }
Run Code Online (Sandbox Code Playgroud)

在视图中我有:

<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>
Run Code Online (Sandbox Code Playgroud)

但那是因为我没有在第三和第四个td上显示医院名称和测试中心名称.我该怎么办呢?我尝试了一些似乎因某些原因似乎无法工作的技术.请指教

Der*_*der 0

如果你尝试这样做会发生什么:

 $this->db->join('hospital', 'hospital.id = clients.id');
 $this->db->join('testing_center', 'testing_center.id = clients.id');
Run Code Online (Sandbox Code Playgroud)

而不是这个:

 $this->db->join('hospital', 'clients.id = hospital.id');
 $this->db->join('testing_center', 'clients.id = testing_center.id');
Run Code Online (Sandbox Code Playgroud)

还检查

client* s * 和 client(如果它们在各处都相同)

并且按照 Nerd 的建议进行更改: $this->db->select('clients.*'); 到:

$this->db->select('*'); 
Run Code Online (Sandbox Code Playgroud)