Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter:连接3个表并在视图中显示数据_Php_Codeigniter - Fatal编程技术网

Php Codeigniter:连接3个表并在视图中显示数据

Php Codeigniter:连接3个表并在视图中显示数据,php,codeigniter,Php,Codeigniter,所以我有3张桌子想加入 我正在构建一个应用程序I Codeigniter,我有3个表 客户端: -id -电话号码 -医院id -smc_状态 -测试中心 医院 -id -名字 测试中心 -id -名字 在模型中,我有以下内容: public function get_clients() { if($slug === FALSE) { $this->db->select('clients.*');

所以我有3张桌子想加入

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

客户端:
-id
-电话号码
-医院id
-smc_状态
-测试中心

医院
-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上显示医院名称和检测中心名称。我该怎么做呢?我尝试了一些由于某种原因似乎不起作用的技术。请告知如果您尝试以下方法会发生什么:

 $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*和客户机,如果它们在任何地方都相同

同时也随着书呆子的提议而改变: $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'] ?>


编辑:还有你,这是
客户端。*
正在做的。更新了我的代码。

应该是这样的

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

我做了更改,但最终得到HTTP错误500。为什么会这样?有很多不同的事情可能出了问题。检查您的PHP日志。我正在本地主机上运行它,无法检查任何日志,但我只是将其更改为:$this->db->select('clients.id,clients.phone\u number,clients.smc\u status,clients.language,hospital.name作为hospital\u name,testing\u center.name作为testing\u center\u name')$此->数据库->来自(“客户”)$这->数据库->加入('hospital','hospital.id=clients.id')$这个->数据库->加入('testing_center','testing_center.id=clients.id')$query=$this->db->get();返回$query->result_array();}$query=$this->db->get_where('clients',array('slug'=>$slug));返回$query->row_array();}我想我拼错了什么。它现在工作得很好。非常感谢!我正在尝试填写一个表单,并将数据推回到数据库中的不同表中。我该怎么做呢?我是先将表单绑定到数据库,以便它理解连接,还是应该将输入文本放置在与数据库中预期内容类似的位置?我已经做了所有必要的更改,首先是Nerd的建议,还有你的建议,但我最终得到了HTTP错误500。这是为什么?我查过了,还犯了一些拼写错误。谢谢你的建议!我正在尝试填写一个表单,并将数据推回到数据库中的不同表中。我该怎么做呢?我是先将表单绑定到数据库,以便它理解连接,还是应该将输入文本放置在与数据库中预期内容类似的位置?在两个表格中连接相同的列名是错误的,例如(“客户中的医院id,医院中的id”)和(“客户中的测试中心id,测试中心中的id”)
 $this->db->join('hospital', 'clients.hospital_id = hospital.id');
 $this->db->join('testing_center', 'clients.testing_center_id  = testing_center.id');