Codeigniter: combining 3 tables and displaying data in the field of view

So, I have 3 tables that I want to join.

I am creating an application i am Codeigniter and I have 3 tables

Client:
-id
-phone_number
-hospital_id
-smc_status
-testing_center_id

Hospital
-id
-name

Testing_center
-id
-name

In the model, I have the following:

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();
    }

In the view, I have:

<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>

But this is because I could not show the name of the hospital and the name of the testing center on the third and fourth td. How can i do this? I tried several techniques that for some reason did not seem to work. Please inform

+5
source share
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'] ?>

EDIT: SELECT *, clients.*. .

+3

, :

 $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');

* s * ,

, Nerd: $ This- > db- > ( ' *.'); :

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

It will be like this

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

All Articles