在php mysql中连接表时出错

时间:2018-05-23 04:39:57

标签: php codeigniter

我想在php mysql中加入表格。这是我的代码:

public public function getResponInfo($kode_laporan)
{
    $this->db->select('*');
    $this->db->from('laporan l, respon_user ru, respon r');
    $this->db->join('admin a', 'a.id_admin = r.id_admin', 'left');
    $this->db->join('respon_user ru', 'ru.kode_laporan = r.kode_laporan', 'left');
    $this->db->join('user u', 'u.id = ru.id_pengirim', 'left');
    $this->db->where("r.kode_laporan = '". $kode_laporan."'");
    //$this->db->order_by('r.tanggal', 'asc');
    return $this->db->get();
}

And it got error like this

我想做的就是显示

' isi_respon'和' tanggal'来自回复表,' nama_opd'来自管理员表,' isi_respon'和' tanggal'来自 respon_user 表格,' nama'来自用户表。

然后,我想通过" tanggal"来订购它们。来自 respon和respon_user 表。
这两个表都响应,respon_user来自 laporan kode_laporan

有可能吗?谢谢你的帮助

更新
我认为你给我的所有代码都在运作。但我有另一个问题,通过控制器和视图文件显示它们。 这是我的控制器代码:

public function detailLaporan($kode_laporan)
{
    $data['page']='detaillaporan';
    $data['laporan'] = $this->Home_model->getLapDetail($kode_laporan);
    $data['id'] = $this->session->userdata('id');
    $data['respon'] = $this->Home_model->getResponInfo($kode_laporan)->result();
    $data['rsp'] = $this->Home_model->getResponInfo($kode_laporan)->num_rows();
    $data['kode_laporan'] = $this->session->set_userdata('kode_laporan');
    $this->load->view('home/master', $data);
}

视图:

if($rsp > 0){
        foreach ($respon as $r) {
           echo $r['tanggal'];
           echo $r['nama']; 
           /*i want to display 'nama' or 'nama_opd', 
             it depends on whether it belongs to 'respon' or 'respon_user' table*/
           $r['isi_respon'];
           }
}

2 个答案:

答案 0 :(得分:0)

请尝试以下代码

$this->db->select('r.isi_respon, r.tanggal as respon_tanggal, a.nama_opd, ru.isi_respon as respon_user_isi_respon, ru.tanggal as response_user_tanggal, u.nama');
$this->db->from('respon r');
$this->db->join('admin a', 'a.id_admin = r.id_admin', 'left');
$this->db->join('respon_user ru', 'ru.kode_laporan = r.kode_laporan', 'left');
$this->db->join('user u', 'u.id = ru.id_pengirim', 'left');
$this->db->where("r.kode_laporan = '". $kode_laporan."'");
$this->db->order_by('r.tanggal, ru.tanggal', 'asc');
return $this->db->get();

答案 1 :(得分:0)

$this->db->select('l.*,ru.*,r.*,a.*,u.*');
$this->db->from('laporan l');
$this->db->join('admin a', 'a.id_admin = r.id_admin', 'left');
$this->db->join('respon_user ru', 'ru.kode_laporan = r.kode_laporan', 'left');
$this->db->join('user u', 'u.id = ru.id_pengirim', 'left');
$this->db->where("r.kode_laporan = '". $kode_laporan."'");
$this->db->order_by('r.tanggal', 'asc');
return $this->db->get();