Postingan lainnya
Bagaimana cara menggunakan where di setiap query berdasarkan id
Ingin menampilkan data dari banyak table di database. Ini script'a:
function edit( $id = null )
{
if($id !='')
if($this->access['is_edit'] ==0) redirect('dashboard',301);
$row = $this->model->getRow( $id );
if($row)
{
$this->data['row'] = $row;
} else {
$this->data['row'] = $this->model->getColumnTable('v_laundry_order');
}
$this->data['id'] = $id;
$this->data['services'] = $this->db->get('v_laundry_service')->result();
$this->data['produks'] = $this->db->get('v_laundry_produk')->result();
$this->data['ukurans'] = $this->db->get('v_laundry_ukuran')->result();
$this->data['warnas'] = $this->db->get('v_laundry_warna')->result();
$this->data['mereks'] = $this->db->get('v_laundry_merek')->result();
$this->data['notes'] = $this->db->get('v_laundry_note')->result();
$this->data['content'] = $this->load->view('laundry_order/form',$this->data, true );
$this->load->view('layouts/main', $this->data );
}
2 Jawaban:
pada dasarnya getdata by id di codeigniter itu mudah, jangan memanggil query lagi pada suatu variable cukup 1x pemanggilan lalu bisa di declar lagi
<pre> function getDataById($key, $value){
$query = $this->db->get_where('mytable', array('$key' => $value));
if(!empty($query->row_array(){ return $query->row_array(); }
return false; } </pre>
untuk pemanggilanya di controller cukup dengan include model databasenya dan dapat dimasukan ke dalam variable.
<pre> //include model $this->load->model('namamodel');
$getDataById = $this->namamodel->getDataById('id', $id_brng);
$data['nama_barang'] = $getDataById['nama_barang']; $data['jumlah'] = $getDataById['jumlah']; $data['keterangan'] = $getDataById['keterangan'];
$this->load->view('layouts/main', $data); </pre>
silahkan untuk di coba dan kirimkan hasilnya bila ada error
Controller'a sudah ok, mas. Tinggal di model'a jadi yg ingin saya tanyakan bagaimana cara bikin query where berdasarkan $array_name = $array_value. Pada model? Script controller dibawah: <pre> function getDataById($id = null) { $id = $this->uri->segment(3); $getDataById = $this->model->getDataById('id', $id);
$data['services'] = $getDataById['v_laundry_service'];
$data['produks'] = $getDataById['v_laundry_produk'];
$data['ukurans'] = $getDataById['v_laundry_ukuran'];
$data['warnas'] = $getDataById['v_laundry_warna'];
$data['mereks'] = $getDataById['v_laundry_merek'];
$data['notes'] = $getDataById['v_laundry_note'];
$this-&gt;data['content'] = $this-&gt;load-&gt;view('laundry_order/form',$this-&gt;data, true );
$this-&gt;load-&gt;view('layouts/main', $data);
}
</pre>
Script di model: <pre> public static function getDataById($array_name, $array_value){
}
</pre>
Tanggapan
array_name, dan array_value kan sama aja dengan $key, $value ?