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

	}

avatar chandraaditya92
@chandraaditya92

3 Kontribusi 0 Poin

Diperbarui 5 tahun yang lalu

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-&gt;db-&gt;get_where('mytable', array('$key' =&gt; $value));

if(!empty($query-&gt;row_array(){ return $query-&gt;row_array(); }

return false; } </pre>

untuk pemanggilanya di controller cukup dengan include model databasenya dan dapat dimasukan ke dalam variable.

<pre> //include model $this-&gt;load-&gt;model('namamodel');

$getDataById = $this-&gt;namamodel-&gt;getDataById('id', $id_brng);

$data['nama_barang'] = $getDataById['nama_barang']; $data['jumlah'] = $getDataById['jumlah']; $data['keterangan'] = $getDataById['keterangan'];

$this-&gt;load-&gt;view('layouts/main', $data); </pre>

silahkan untuk di coba dan kirimkan hasilnya bila ada error

avatar maszgalang
@maszgalang

297 Kontribusi 80 Poin

Dipost 5 tahun yang lalu

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-&gt;uri-&gt;segment(3); $getDataById = $this-&gt;model-&gt;getDataById('id', $id);

	$data[&#039;services&#039;] = $getDataById[&#039;v_laundry_service&#039;];
	$data[&#039;produks&#039;] = $getDataById[&#039;v_laundry_produk&#039;];
	$data[&#039;ukurans&#039;] = $getDataById[&#039;v_laundry_ukuran&#039;];
	$data[&#039;warnas&#039;] = $getDataById[&#039;v_laundry_warna&#039;];
	$data[&#039;mereks&#039;] = $getDataById[&#039;v_laundry_merek&#039;];
	$data[&#039;notes&#039;] = $getDataById[&#039;v_laundry_note&#039;];
	
	$this-&amp;gt;data[&#039;content&#039;] = $this-&amp;gt;load-&amp;gt;view(&#039;laundry_order/form&#039;,$this-&amp;gt;data, true );
	
	$this-&amp;gt;load-&amp;gt;view(&#039;layouts/main&#039;, $data);
	
}

</pre>

Script di model: <pre> public static function getDataById($array_name, $array_value){

}

</pre>

avatar chandraaditya92
@chandraaditya92

3 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

Tanggapan

array_name, dan array_value kan sama aja dengan $key, $value ?

Login untuk ikut Jawaban