通过ID获取数据详细信息 - CODEIGNITER

时间:2018-02-21 05:15:50

标签: php codeigniter phpmyadmin codeigniter-2 codeigniter-3

当我通过id获取数据时,只显示表中的第一行

这是我的控制器

public function getDetail($id=true)
{

    $data['detail'] = $this->vendor_model->getDetail($id);
    $this->load->view('vendor/detailvendor',$data);

}

这是我的模特

 public function getDetail($id)
{

    $this->db->where('no_spk',$id);
    $query = $this->db->query("select a.no_spk, a.nama_pekerjaan, a.alamat, a.latitude, a.longitude, b.nama_vendor, c.progress, c.keterangan, c.gambar, c.gambar2, c.gambar3, d.namapekerja from vendor as b inner join pekerjaan as a on b.id_vendor = a.id_vendor inner join progress as c on c.no_spk = a.no_spk inner join pekerja as d on d.id_pekerja=c.id_pekerja");
    return $query->result();
}

而且,这是我的观点

<?php echo form_open_multipart('homevendor/getDetail'.$this->uri->segment(4)); ?>
<?php echo $detail[0]->no_spk?><br>
<?php echo $detail[0]->nama_pekerjaan?><br>
<?php echo $detail[0]->progress?><br>
<?php echo form_close();?>

结果是view on website content of table, there are 2 data but only show first data

请帮帮我

2 个答案:

答案 0 :(得分:1)

1.要获取并显示所有记录,只需更改view文件代码,如下所示: -

 <?php echo form_open_multipart('homevendor/getDetail'.$this->uri->segment(4)); ?>
  <?php foreach($detail as $deta){?>
    <?php echo $deta->no_spk?><br>
    <?php echo $deta->nama_pekerjaan?><br>
    <?php echo $deta->progress?><br>
  <?php }?>
<?php echo form_close();?>

2.要获取和显示单个记录,请在查询中使用LIMI 1并更改视图代码,如下所示: -

模态代码: -

public function getDetail($id){
   $this->db->where('no_spk',$id);
   $query = $this->db->query("select a.no_spk, a.nama_pekerjaan, a.alamat, a.latitude, a.longitude, b.nama_vendor, c.progress, c.keterangan, c.gambar, c.gambar2, c.gambar3, d.namapekerja from vendor as b inner join pekerjaan as a on b.id_vendor = a.id_vendor inner join progress as c on c.no_spk = a.no_spk inner join pekerja as d on d.id_pekerja=c.id_pekerja LIMIT 1");
   return $query->result();
}

并查看: -

<?php echo form_open_multipart('homevendor/getDetail'.$this->uri->segment(4)); ?>
  <?php foreach($detail as $deta){?>
    <?php echo $deta->no_spk?><br>
    <?php echo $deta->nama_pekerjaan?><br>
    <?php echo $deta->progress?><br>
  <?php }?>
<?php echo form_close();?>

答案 1 :(得分:1)

    public function getDetail($id)
{

    //$this->db->where('no_spk',$id);
    $query = $this->db->query("select a.no_spk, a.nama_pekerjaan, a.alamat, a.latitude, a.longitude, b.nama_vendor, c.progress, c.keterangan, c.gambar, c.gambar2, c.gambar3, d.namapekerja from vendor as b inner join pekerjaan as a on b.id_vendor = a.id_vendor inner join progress as c on c.no_spk = a.no_spk inner join pekerja as d on d.id_pekerja=c.id_pekerja WHERE c.no_spk=".$id);
    return $query->row();
}

查看

<?php echo form_open_multipart('homevendor/getDetail'.$this->uri->segment(4)); ?>
<?php echo $detail->no_spk?><br>
<?php echo $detail->nama_pekerjaan?><br>
<?php echo $detail->progress?><br>
<?php echo form_close();?>

解释

您正在寻找获取单行。使用行函数而不是结果。 Codeigniter数据库类行函数返回一个对象。所以你可以使用 - &gt;访问对象数据。