Contoh Kasus Sederhana Create Read Update Delete(CRUD) Part 2

Operasi Database Pada Tabel Hasil_Opname

Operasi Create (Menambah Data)

Setelah operasi database pada tabel entry_buku_kas selesai,kita tinggal memodifikasi yang ada dengan menambahkan atau mengurangi file-file yang sudah kita buat sebelumnya. Langkah selanjutnya kita mencoba membuat operasi database pada tabel hasil_opname, diawali dengan penambahan data/insert data pada tabel hasil_opname.

Pertama buat file model dengan nama mopnamekasbank.php . Simpan pada C:\xampp\htdocs\bukukas\application\models. Dengan script sebagai berikut.

<?php
class MopnameKasBank extends CI_Model {	
	function tambah(){
		$tgl_entry = $this->input->post('tgl_opname'); 
		$uk100rb =  $this->input->post('uk100rb'); 
		$data1 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk100rb',
						'nilai'=>$uk100rb); 
		$this->db->insert('hasil_opname',$data1);
		
		$uk50rb =  $this->input->post('uk50rb'); 
		$data2 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk50rb',
						'nilai'=>$uk50rb); 
		$this->db->insert('hasil_opname',$data2);
		
		$uk20rb =  $this->input->post('uk20rb'); 
		$data3 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk20rb',
						'nilai'=>$uk20rb); 
		$this->db->insert('hasil_opname',$data3);
		
		$uk10rb =  $this->input->post('uk10rb'); 
		$data4 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk10rb',
						'nilai'=>$uk10rb); 
		$this->db->insert('hasil_opname',$data4);
		
		$uk5rb =  $this->input->post('uk5rb'); 
		$data5 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk5rb',
						'nilai'=>$uk5rb); 
		$this->db->insert('hasil_opname',$data5);
		
		$uk2rb =  $this->input->post('uk2rb'); 
		$data6 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk2rb',
						'nilai'=>$uk2rb); 
		$this->db->insert('hasil_opname',$data6);
		
		$uk1rb =  $this->input->post('uk1rb'); 
		$data7 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk1rb',
						'nilai'=>$uk1rb); 
		$this->db->insert('hasil_opname',$data7);
		
		$uk5rts =  $this->input->post('uk5rts'); 
		$data8 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk5rts',
						'nilai'=>$uk5rts); 
		$this->db->insert('hasil_opname',$data8);
		
		$uk1rts =  $this->input->post('uk1rts'); 
		$data9 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk1rts',
						'nilai'=>$uk1rts);
		$this->db->insert('hasil_opname',$data9);
		
		$ul1rb =  $this->input->post('ul1rb'); 
		$data10 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul1rb',
						'nilai'=>$ul1rb); 
		$this->db->insert('hasil_opname',$data10);
		
		$ul5rts =  $this->input->post('ul5rts'); 
		$data11 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul5rts',
						'nilai'=>$ul5rts); 
		$this->db->insert('hasil_opname',$data11);
		
		$ul2rts =  $this->input->post('ul2rts'); 
		$data12 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul2rts',
						'nilai'=>$ul2rts); 
		$this->db->insert('hasil_opname',$data12);
		
		$ul5plh =  $this->input->post('ul5plh'); 
		$data13 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul5plh',
						'nilai'=>$ul5plh); 
		$this->db->insert('hasil_opname',$data13);
		
		$ul1rts =  $this->input->post('ul1rts'); 
		$data14 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul1rts',
						'nilai'=>$ul1rts); 
		$this->db->insert('hasil_opname',$data14);
		
		$ul25plh =  $this->input->post('ul25plh'); 
		$data15 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul25plh',
						'nilai'=>$ul25plh); 
		$this->db->insert('hasil_opname',$data15);
		
	}	
}
?> 

Kedua buat file controller dengan nama copnamekasbank.php. Simpan pada
C:\xampp\htdocs\bukukas\application\controllers
. Dengan script sebagai berikut.

<?php
class CopnameKasBank extends CI_Controller {
	function tambahdata(){
		
		if($this->input->post('submit')){
			$this->load->model('mopnamekasbank');
			$this->mopnamekasbank->tambah();
			redirect('copnamekasbank/index');
		}
		$this->load->view('ventryopnamekasbank');
	}			
}
?> 

Ketiga buat file view dengan nama ventryopnamekasbank.php. Simpan pada
C:\xampp\htdocs\bukukas\application\views
. Dengan script sebagai berikut.

<html>	
<head>
	<title>Menambahkan Data</title>
</head>    
<body>
	<h3>Tambah Data</h3>
    <?php
    echo form_open('copnamekasbank/tambahdata');
    ?>
<table>
<?php
$no = 1;
?>
	<tr>
		<td>No</td>
		<td>Kode Rincian Uang</td>			
		<td>Jumlah</td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>uk100rb</td>
		<td><?php echo form_input('uk100rb'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>uk50rb</td>
		<td><?php echo form_input('uk50rb'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>uk20rb</td>
		<td><?php echo form_input('uk20rb'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>uk10rb</td>
		<td><?php echo form_input('uk10rb'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>uk5rb</td>
		<td><?php echo form_input('uk5rb'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>uk2rb</td>
		<td><?php echo form_input('uk2rb'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>uk1rb</td>
		<td><?php echo form_input('uk1rb'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>uk5rts</td>
		<td><?php echo form_input('uk5rts'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>uk1rts</td>
		<td><?php echo form_input('uk1rts'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>ul1rb</td>
		<td><?php echo form_input('ul1rb'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>ul5rts</td>
		<td><?php echo form_input('ul5rts'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>ul2rts</td>
		<td><?php echo form_input('ul2rts'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>ul5plh</td>
		<td><?php echo form_input('ul5plh'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>ul1rts</td>
		<td><?php echo form_input('ul1rts'); ?></td>
	</tr>
	<tr>
		<td><?php echo $no++;?></td>
		<td>ul25plh</td>
		<td><?php echo form_input('ul25plh'); ?></td>
	</tr>

</table>    
<?php echo form_close(); ?>
</body>
</html>

Untuk mengujikan ketikan pada browser url http://localhost/bukukas/index.php/copnamekasbank/tambahdata.

untuk mengecek data sudah berhasil atau belum disimpan, buka phpmyadmin.

Operasi Read (Menampilkan Data)

Untuk menampilkan data yang telah diisikan pada form Tambah Data, buka kembali file model bernama mopnamekasbank.php, kemudian tambahkan function bacadata() :

 <?php
class MopnameKasBank extends CI_Model {
	function bacadata(){
		$baca = $this->db->get('hasil_opname');
		if($baca->num_rows() > 0){
			foreach ($baca->result() as $data){
				$hasil[] = $data;
			}
			return $hasil;
		}
	}
	
	function tambah(){
		$tgl_entry = $this->input->post('tgl_opname'); 
		$uk100rb =  $this->input->post('uk100rb'); 
		$data1 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk100rb',
						'nilai'=>$uk100rb); 
		$this->db->insert('hasil_opname',$data1);
		
		$uk50rb =  $this->input->post('uk50rb'); 
		$data2 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk50rb',
						'nilai'=>$uk50rb); 
		$this->db->insert('hasil_opname',$data2);
		
		$uk20rb =  $this->input->post('uk20rb'); 
		$data3 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk20rb',
						'nilai'=>$uk20rb); 
		$this->db->insert('hasil_opname',$data3);
		
		$uk10rb =  $this->input->post('uk10rb'); 
		$data4 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk10rb',
						'nilai'=>$uk10rb); 
		$this->db->insert('hasil_opname',$data4);
		
		$uk5rb =  $this->input->post('uk5rb'); 
		$data5 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk5rb',
						'nilai'=>$uk5rb); 
		$this->db->insert('hasil_opname',$data5);
		
		$uk2rb =  $this->input->post('uk2rb'); 
		$data6 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk2rb',
						'nilai'=>$uk2rb); 
		$this->db->insert('hasil_opname',$data6);
		
		$uk1rb =  $this->input->post('uk1rb'); 
		$data7 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk1rb',
						'nilai'=>$uk1rb); 
		$this->db->insert('hasil_opname',$data7);
		
		$uk5rts =  $this->input->post('uk5rts'); 
		$data8 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk5rts',
						'nilai'=>$uk5rts); 
		$this->db->insert('hasil_opname',$data8);
		
		$uk1rts =  $this->input->post('uk1rts'); 
		$data9 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'uk1rts',
						'nilai'=>$uk1rts);
		$this->db->insert('hasil_opname',$data9);
		
		$ul1rb =  $this->input->post('ul1rb'); 
		$data10 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul1rb',
						'nilai'=>$ul1rb); 
		$this->db->insert('hasil_opname',$data10);
		
		$ul5rts =  $this->input->post('ul5rts'); 
		$data11 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul5rts',
						'nilai'=>$ul5rts); 
		$this->db->insert('hasil_opname',$data11);
		
		$ul2rts =  $this->input->post('ul2rts'); 
		$data12 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul2rts',
						'nilai'=>$ul2rts); 
		$this->db->insert('hasil_opname',$data12);
		
		$ul5plh =  $this->input->post('ul5plh'); 
		$data13 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul5plh',
						'nilai'=>$ul5plh); 
		$this->db->insert('hasil_opname',$data13);
		
		$ul1rts =  $this->input->post('ul1rts'); 
		$data14 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul1rts',
						'nilai'=>$ul1rts); 
		$this->db->insert('hasil_opname',$data14);
		
		$ul25plh =  $this->input->post('ul25plh'); 
		$data15 = array('tgl_opname'=>$tgl_entry,
						'kd_rincian_uang'=>'ul25plh',
						'nilai'=>$ul25plh); 
		$this->db->insert('hasil_opname',$data15);
		
	}	
}
?> 

Selanjutnya
buka kembali file controller copnamekasbank.php dan lakukan juga penambahan function index() :

<?php
class CopnameKasBank extends CI_Controller {

	function index(){
		$this->load->model('mopnamekasbank');
		$data['hasil'] = $this->mopnamekasbank->bacadata();
		$this->load->view('vbacaopnamekasbank',$data);
	}
	function tambahdata(){
		
		if($this->input->post('submit')){
			$this->load->model('mopnamekasbank');
			$this->mopnamekasbank->tambah();
			redirect('copnamekasbank/index');
		}
		$this->load->view('ventryopnamekasbank');
	}			
}
?> 
  

Ketiga buat file view dengan nama vbacaopnamekasbank.php. Simpan pada
C:\xampp\htdocs\bukukas\application\views
. Dengan script sebagai berikut.

<html>	
<head>
	<title>Membaca Data</title>
</head>    
<body>
	<h3><?php echo anchor('copnamekasbank/tambahdata','Tambah data') ?></h3>
	<h3>Baca Data</h3>
    <?php
	if(empty($hasil)){
		echo "Tidak ada data";
	}    
	else{
	?>
	<table border='1'>
		<tr>
			<td>No</td>
			<td>Tgl Entry</td>
			<td>Kode Rincian Uang</td>			
			<td>Jumlah</td>
		</tr>
		<?php
		$no = 1;
		foreach($hasil as $data) {
		?>
		<tr>
			<td><?php echo $no++ ;?></td>
			<td><?php echo $data->tgl_opname ;?></td>
			<td><?php echo $data->kd_rincian_uang;?></td>			
			<td><?php echo $data->nilai ;?></td>
		</tr>
		<?php
		}
		?>
		</table>    
	
	<?php	
	}
    ?>

</body>
</html>

kemudian jalankan pada browser ketik kan url http://localhost/bukukas/index.php/copnamekasbank

Operasi Update (Mengubah Data)

Ada kalanya data yang telah diinput tersebut diperbaharui atau salah entry. Proses update tidak perlu mengentry dari awal,cukup panggil data yang ada,rubah bagian tertentu, kemudian simpan lagi. Untuk membuat skrip update, tambahkan function filterdata() untuk memanggil data yang akan diupdate berdasarkan kriteria, dan function updatedata() untuk merubah data hasil pengupdate-an pada model mopnamekasbank.php seperti dibawah ini:

  <?php
class MopnameKasBank extends CI_Model {	
   	function updatedata(){
		$tgl_opname = $this->input->post('tgl_opname');
		$kd_rincian_uang = $this->input->post('kd_rincian_uang');
		$nilai = $this->input->post('nilai');
		$data = array(
				'tgl_opname'=>$tgl_opname,
				'kd_rincian_uang'=>$kd_rincian_uang,
				'nilai'=>$nilai
				);		
		$this->db->where(array('tgl_opname'=>$tgl_opname,
								'kd_rincian_uang'=>$kd_rincian_uang));			
		$this->db->update('hasil_opname',$data);		
	}

    
	function filterdata($tgl_opname,$kd_rincian_uang){
		return $this->db->get_where('hasil_opname',
		                  array('tgl_opname'=>$tgl_opname,
								'kd_rincian_uang'=>$kd_rincian_uang))->row();		
	}
	
	function bacadata(){
		$baca = $this->db->get('hasil_opname');
		if($baca->num_rows() > 0){
			foreach ($baca->result() as $data){
				$hasil[] = $data;
			}
			return $hasil;
		}
	}
	
	function tambah(){
		$kd_rincian_uang = $this->input->post('tgl_opname'); 
		$uk100rb =  $this->input->post('uk100rb'); 
		$data1 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk100rb',
						'nilai'=>$uk100rb); 
		$this->db->insert('hasil_opname',$data1);
		
		$uk50rb =  $this->input->post('uk50rb'); 
		$data2 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk50rb',
						'nilai'=>$uk50rb); 
		$this->db->insert('hasil_opname',$data2);
		
		$uk20rb =  $this->input->post('uk20rb'); 
		$data3 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk20rb',
						'nilai'=>$uk20rb); 
		$this->db->insert('hasil_opname',$data3);
		
		$uk10rb =  $this->input->post('uk10rb'); 
		$data4 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk10rb',
						'nilai'=>$uk10rb); 
		$this->db->insert('hasil_opname',$data4);
		
		$uk5rb =  $this->input->post('uk5rb'); 
		$data5 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk5rb',
						'nilai'=>$uk5rb); 
		$this->db->insert('hasil_opname',$data5);
		
		$uk2rb =  $this->input->post('uk2rb'); 
		$data6 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk2rb',
						'nilai'=>$uk2rb); 
		$this->db->insert('hasil_opname',$data6);
		
		$uk1rb =  $this->input->post('uk1rb'); 
		$data7 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk1rb',
						'nilai'=>$uk1rb); 
		$this->db->insert('hasil_opname',$data7);
		
		$uk5rts =  $this->input->post('uk5rts'); 
		$data8 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk5rts',
						'nilai'=>$uk5rts); 
		$this->db->insert('hasil_opname',$data8);
		
		$uk1rts =  $this->input->post('uk1rts'); 
		$data9 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk1rts',
						'nilai'=>$uk1rts);
		$this->db->insert('hasil_opname',$data9);
		
		$ul1rb =  $this->input->post('ul1rb'); 
		$data10 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul1rb',
						'nilai'=>$ul1rb); 
		$this->db->insert('hasil_opname',$data10);
		
		$ul5rts =  $this->input->post('ul5rts'); 
		$data11 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul5rts',
						'nilai'=>$ul5rts); 
		$this->db->insert('hasil_opname',$data11);
		
		$ul2rts =  $this->input->post('ul2rts'); 
		$data12 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul2rts',
						'nilai'=>$ul2rts); 
		$this->db->insert('hasil_opname',$data12);
		
		$ul5plh =  $this->input->post('ul5plh'); 
		$data13 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul5plh',
						'nilai'=>$ul5plh); 
		$this->db->insert('hasil_opname',$data13);
		
		$ul1rts =  $this->input->post('ul1rts'); 
		$data14 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul1rts',
						'nilai'=>$ul1rts); 
		$this->db->insert('hasil_opname',$data14);
		
		$ul25plh =  $this->input->post('ul25plh'); 
		$data15 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul25plh',
						'nilai'=>$ul25plh); 
		$this->db->insert('hasil_opname',$data15);
		
	}	
}
?> 
  

Selanjutnya buka kembali file controller copnamekasbank.php untuk menambahkan function updatedata(), dengan script sebagai berikut.

<?php
class CopnameKasBank extends CI_Controller {
	function updatedata($tgl_opname,$kd_rincian_uang){
		
		if($_POST==NULL){
			$this->load->model('mopnamekasbank');
			$data['hasil'] = $this->mopnamekasbank->filterdata($tgl_opname,$kd_rincian_uang);			
			$this->load->view('vupdateopnamekasbank',$data);
		}	
		else{
			$this->load->model('mopnamekasbank');
			$this->mopnamekasbank->updatedata();
			redirect('copnamekasbank/index');
		}
	}

	function index(){
		$this->load->model('mopnamekasbank');
		$data['hasil'] = $this->mopnamekasbank->bacadata();
		$this->load->view('vbacaopnamekasbank',$data);
	}
	function tambahdata(){
		
		if($this->input->post('submit')){
			$this->load->model('mopnamekasbank');
			$this->mopnamekasbank->tambah();
			redirect('copnamekasbank/index');
		}
		$this->load->view('ventryopnamekasbank');
	}			
}
?> 
  

Selanjutnya buat file baru bernama vupdatebukukas.php,letakan pada folder view. Dengan scrip sebagai berikut :

  <html>	
<head>
	<title>Mengupdate Data</title>
</head>    
<body>
	<h3>Update Data</h3>
    <table border='1'>	
	<?php
	echo form_open('copnamekasbank/updatedata/'.$hasil->tgl_opname.'/'.$hasil->kd_rincian_uang);
	?>
		<tr>
			<td>Tgl Opname</td>
			<td>:</td>
			<td><?php echo form_input('tgl_opname', $hasil->tgl_opname);?></td>
		</tr>	
		<tr>	
			<td>Kode Rincian Uang</td>
			<td>:</td>
			<td><?php echo form_input('kd_rincian_uang', $hasil->kd_rincian_uang);?></td>
		</tr>	
		<tr>	
			<td>Jumlah Uang</td>
			<td>:</td>
			<td><?php echo form_input('nilai', $hasil->nilai);?></td>
		</tr>
		<tr>				
			<td><?php echo form_submit('submit','Update', 'id="submit"');?></td>
		</tr>
		</table>    	
		<?php form_close(); ?>
</body>
</html>

Selanjutnya buka kembali file vbacabukukas.php, sesuaikan dengan scrip berikut ini.

<html>	
<head>
	<title>Membaca Data</title>
</head>    
<body>
	<h3><?php echo anchor('copnamekasbank/tambahdata','Tambah data') ?></h3>
	<h3>Baca Data</h3>
    <?php
	if(empty($hasil)){
		echo "Tidak ada data";
	}    
	else{
	?>
	<table border='1'>
		<tr>
			<td>No</td>
			<td>Tgl Entry</td>
			<td>Kode Rincian Uang</td>			
			<td>Jumlah</td>
			<td>Aksi</td>
		</tr>
		<?php
		$no = 1;
		foreach($hasil as $data) {
		?>
		<tr>
			<td><?php echo $no++ ;?></td>
			<td><?php echo $data->tgl_opname ;?></td>
			<td><?php echo $data->kd_rincian_uang;?></td>			
			<td><?php echo $data->nilai ;?></td>
			<td><a href="updatedata/<?php echo $data->tgl_opname ;?>/<?php echo $data->kd_rincian_uang ;?>">
				Update</a></td>
		</tr>
		<?php
		}
		?>
		</table>    
	
	<?php	
	}
    ?>

</body>
</html>

kemudian jalankan pada browser ketik kan url http://localhost/bukukas/index.php/copnamekasbank/index

Operasi Delete (Menghapus Data)

Dibanding beberapa proses sebelumnya,proses delete lah yang paling sedikit skripnya. Untuk membuat proses ini buka file model mopnamekasbank.php. Tambahkan function deletedata() dengan scrip sebagai berikut.

  <?php
class MopnameKasBank extends CI_Model {

	function deletedata($tgl_opname,$kd_rincian_uang){		
		$data = array(
				'tgl_opname'=>$tgl_opname,
				'kd_rincian_uang'=>$kd_rincian_uang
				);		
		$this->db->delete('hasil_opname',$data);		
	}
	
	function updatedata(){
		$tgl_opname = $this->input->post('tgl_opname');
		$kd_rincian_uang = $this->input->post('kd_rincian_uang');
		$nilai = $this->input->post('nilai');
		$data = array(
				'tgl_opname'=>$tgl_opname,
				'kd_rincian_uang'=>$kd_rincian_uang,
				'nilai'=>$nilai
				);		
		$this->db->where('tgl_opname',$tgl_opname,'kd_rincian_uang',$kd_rincian_uang);		
		$this->db->update('hasil_opname',$data);		
	}
	
	function filterdata($tgl_opname,$kd_rincian_uang){
		return $this->db->get_where('hasil_opname',
		                  array('tgl_opname'=>$tgl_opname,
								'kd_rincian_uang'=>$kd_rincian_uang))->row();		
	}
	
	function bacadata(){
		$baca = $this->db->get('hasil_opname');
		if($baca->num_rows() > 0){
			foreach ($baca->result() as $data){
				$hasil[] = $data;
			}
			return $hasil;
		}
	}
	
	function tambah(){
		$kd_rincian_uang = $this->input->post('tgl_opname'); 
		$uk100rb =  $this->input->post('uk100rb'); 
		$data1 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk100rb',
						'nilai'=>$uk100rb); 
		$this->db->insert('hasil_opname',$data1);
		
		$uk50rb =  $this->input->post('uk50rb'); 
		$data2 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk50rb',
						'nilai'=>$uk50rb); 
		$this->db->insert('hasil_opname',$data2);
		
		$uk20rb =  $this->input->post('uk20rb'); 
		$data3 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk20rb',
						'nilai'=>$uk20rb); 
		$this->db->insert('hasil_opname',$data3);
		
		$uk10rb =  $this->input->post('uk10rb'); 
		$data4 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk10rb',
						'nilai'=>$uk10rb); 
		$this->db->insert('hasil_opname',$data4);
		
		$uk5rb =  $this->input->post('uk5rb'); 
		$data5 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk5rb',
						'nilai'=>$uk5rb); 
		$this->db->insert('hasil_opname',$data5);
		
		$uk2rb =  $this->input->post('uk2rb'); 
		$data6 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk2rb',
						'nilai'=>$uk2rb); 
		$this->db->insert('hasil_opname',$data6);
		
		$uk1rb =  $this->input->post('uk1rb'); 
		$data7 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk1rb',
						'nilai'=>$uk1rb); 
		$this->db->insert('hasil_opname',$data7);
		
		$uk5rts =  $this->input->post('uk5rts'); 
		$data8 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk5rts',
						'nilai'=>$uk5rts); 
		$this->db->insert('hasil_opname',$data8);
		
		$uk1rts =  $this->input->post('uk1rts'); 
		$data9 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'uk1rts',
						'nilai'=>$uk1rts);
		$this->db->insert('hasil_opname',$data9);
		
		$ul1rb =  $this->input->post('ul1rb'); 
		$data10 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul1rb',
						'nilai'=>$ul1rb); 
		$this->db->insert('hasil_opname',$data10);
		
		$ul5rts =  $this->input->post('ul5rts'); 
		$data11 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul5rts',
						'nilai'=>$ul5rts); 
		$this->db->insert('hasil_opname',$data11);
		
		$ul2rts =  $this->input->post('ul2rts'); 
		$data12 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul2rts',
						'nilai'=>$ul2rts); 
		$this->db->insert('hasil_opname',$data12);
		
		$ul5plh =  $this->input->post('ul5plh'); 
		$data13 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul5plh',
						'nilai'=>$ul5plh); 
		$this->db->insert('hasil_opname',$data13);
		
		$ul1rts =  $this->input->post('ul1rts'); 
		$data14 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul1rts',
						'nilai'=>$ul1rts); 
		$this->db->insert('hasil_opname',$data14);
		
		$ul25plh =  $this->input->post('ul25plh'); 
		$data15 = array('tgl_opname'=>$kd_rincian_uang,
						'kd_rincian_uang'=>'ul25plh',
						'nilai'=>$ul25plh); 
		$this->db->insert('hasil_opname',$data15);
		
	}	
}
?> 

  

Penjelasan kode :

$this->db->delete(‘hasil_opname’,$data); untuk mendelete data dengan parameter array $data.

Selanjutnya buka kembali file controller copnamekasbank.php untuk menambahkan function deletedata(), dengan script sebagai berikut.

  <?php
class CopnameKasBank extends CI_Controller {

	function deletedata($tgl_entry,$kd_rincian_uang){				
			$this->load->model('mopnamekasbank');			
			$this->mopnamekasbank->deletedata($tgl_opname,$kd_rincian_uang);
			redirect('copnamekasbank/index');
	}


	function updatedata($tgl_opname,$kd_rincian_uang){
		
		if($_POST==NULL){
			$this->load->model('mopnamekasbank');
			$data['hasil'] = $this->mopnamekasbank->filterdata($tgl_opname,$kd_rincian_uang);			
			$this->load->view('vupdateopnamekasbank',$data);
		}	
		else{
			$this->load->model('mopnamekasbank');
			$this->mopnamekasbank->updatedata();
			redirect('copnamekasbank/index');
		}
	}

	function index(){
		$this->load->model('mopnamekasbank');
		$data['hasil'] = $this->mopnamekasbank->bacadata();
		$this->load->view('vbacaopnamekasbank',$data);
	}
	function tambahdata(){
		
		if($this->input->post('submit')){
			$this->load->model('mopnamekasbank');
			$this->mopnamekasbank->tambah();
			redirect('copnamekasbank/index');
		}
		$this->load->view('ventryopnamekasbank');
	}			
}
?>  
  

Selanjutnya buka kembali file vbacabukukas.php, sesuaikan dengan scrip berikut ini.

  <html>	
<head>
	<title>Membaca Data</title>
</head>    
<body>
	<h3><?php echo anchor('copnamekasbank/tambahdata','Tambah data') ?></h3>
	<h3>Baca Data</h3>
    <?php
	if(empty($hasil)){
		echo "Tidak ada data";
	}    
	else{
	?>
	<table border='1'>
		<tr>
			<td>No</td>
			<td>Tgl Entry</td>
			<td>Kode Rincian Uang</td>			
			<td>Jumlah</td>
			<td>Aksi</td>
		</tr>
		<?php
		$no = 1;
		foreach($hasil as $data) {
		?>
		<tr>
			<td><?php echo $no++ ;?></td>
			<td><?php echo $data->tgl_opname ;?></td>
			<td><?php echo $data->kd_rincian_uang;?></td>			
			<td><?php echo $data->nilai ;?></td>
			<td><a href="copnamekasbank/updatedata/<?php echo $data->tgl_opname ;?>/<?php echo $data->kd_rincian_uang ;?>">
				Update</a> | 
				<a href="copnamekasbank/deletedata/<?php echo $data->tgl_opname ;?>/<?php echo $data->kd_rincian_uang ;?>">
				Delete</a>
			</td>
		</tr>
		<?php
		}
		?>
		</table>    
	
	<?php	
	}
    ?>

</body>
</html>
    

kemudian jalankan pada browser ketik kan url http://localhost/bukukas/index.php/copnamekasbank/index

Selanjutnya akan dilanjutkan dengan 1 tabel yang lain.

https://javanewbie.wordpress.com/php/framework/codeigniter/contoh-kasus-sederhana-create-read-update-deletecrud-part-3/

  1. 26 August 2016 at 7:51 am

    A Database Error Occurred

    Error Number: 1048

    Column ‘tgl_opname’ cannot be null

    INSERT INTO `hasil_opname` (`tgl_opname`, `kd_rincian_uang`, `nilai`) VALUES (NULL, ‘uk100rb’, ‘909’)

    Filename: C:/xampp/htdocs/bukukas1/system/database/DB_driver.php

    Line Number: 691

    KENAPA ITU GAN KO MALAH GTU??

    • javanewbie
      28 August 2016 at 4:33 am

      coba dicek name attribut tgl_opnamenya, apakah terbawa hasil post atau tidak, kemungkinan penamaan di html tidak sama saat melakukan event post.
      jadi langkah yang dilakukan :
      1. cek name pada view.
      2. cek string post pada controller.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: