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


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
function getDataById($key, $value){

$query = $this->db->get_where('mytable', array('$key' => $value));

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

return false;
}
untuk pemanggilanya di controller cukup dengan include model databasenya dan dapat dimasukan ke dalam variable.
//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);
silahkan untuk di coba dan kirimkan hasilnya bila ada error
avatar maszgalang

@maszgalang

297 Kontribusi 80 Poin


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:
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->data['content'] = $this->load->view('laundry_order/form',$this->data, true );
		
		$this->load->view('layouts/main', $data);
		
	}
Script di model:
public static function getDataById($array_name, $array_value){
		
		
	}
avatar chandraaditya92

@chandraaditya92

3 Kontribusi 0 Poin

  • array_name, dan array_value kan sama aja dengan $key, $value ? - @maszgalang


Login untuk gabung berdiskusi