Kali ini kita gunakan Helper Form untuk membuat halaman edit. Buatlah file view veditguru.php pada direktori webci/application/views/veditguru.php seperti ini :
<?php echo form_open_multipart('guru/ubah/'.$baris->nip,array('method'=>'post')) ?> <table> <tr><td>NIP</td><td><?php echo form_input(array('name'=>'nip','value'=>$baris->nip,'size'=>'20','maxlength'=>'20')); ?></td></tr> <tr><td>Nama</td><td><?php echo form_input('nama',$baris->nama); ?></td></tr> <tr><td>Alamat</td><td><?php echo form_input('alamat',$baris->alamat); ?></td></tr> <tr><td>Tgl Lahir</td><td><?php echo form_input('tgllahir',$baris->tgllahir); ?></td></tr> <tr><td>Tunjangan</td><td><?php echo form_input('tunjangan',$baris->tunjangan); ?></td></tr> <!--tr><td>Agama</td><td> <?php /* $options = array( ''=>'pilih', 'Islam'=>'Islam', 'Kristen'=>'Kristen', 'Katolik'=>'Katolik', 'Hindu'=>'Hindu', 'Budha'=>'Budha' ); echo form_dropdown('agama',$options,$baris->agama); */ ?> </td></tr> <tr><td>Departemen</td><td> <?php /* $options = array(''=>'pilih'); foreach($barisdepartemen as $row) { $options[$row->kddepartemen] = $row->nmdepartemen; } echo form_dropdown('kddepartemen',$options,$baris->kddepartemen); */ ?> </td></tr> <tr><td>Foto</td><td><input type="file" name="userfile" size="20" /></td></tr--> <tr><td></td><td><?php echo form_submit(array('value'=>'Simpan','name'=>'submit','id'=>'submit')); ?></td></tr> </table> <?php echo form_close(); ?>
Kemudian tambahkan kode program ini pada model mguru.php yang ada di webci/application/models/mguru :
<?php class Mguru extends CI_Model { function getdata() { //$this->load->library('table'); //$query = $this->db->query("select * from guru"); //return $this->table->generate($query); //-------------------- //$ambil = $this->db->get('guru'); //-------------------- $this->db->select('*'); $this->db->from('guru'); //$this->db->join('departemen','karyawan.kddepartemen = departemen.kddepartemen','left'); $ambil = $this->db->get(); $this->table->set_empty(' '); $this->table->set_heading('No','NIP','Nama','Alamat','Tgl Lahir','Tunjangan','Foto','Action'); $i=0; if($ambil->num_rows() > 0) { foreach($ambil->result() as $baris) { $config_img = array('src'=>'images/'.$baris->foto,'width'=>'100'); $i++; $this->table->add_row($i,$baris->nip,$baris->nama,$baris->alamat,$baris->tgllahir,$baris->tunjangan,img($config_img),anchor('guru/ubah/'.$baris->nip,'Edit',array('class'=>'linkmerah')).' '.anchor('guru/hapus/'.$baris->nip,'Hapus', array("onClick"=>"return confirm('Anda Yakin?');"))); } return $this->table->generate(); } } function simpandata(){ $nip = $this->input->post('nip'); $nama = $this->input->post('nama'); $alamat = $this->input->post('alamat'); $tgllahir = $this->input->post('tgllahir'); $tunjangan = $this->input->post('tunjangan'); //$agama = $this->input->post('agama'); //$kddepartemen = $this->input->post('kddepartemen'); $data = array( 'nip' => $nip, 'nama' => $nama, 'alamat' => $alamat, 'tgllahir' => $tgllahir, 'tunjangan' => $tunjangan //'agama' => $agama, //'kddepartemen' => $kddepartemen ); $this->db->insert('guru',$data); } function selectdata($id){ return $this->db->get_where('guru',array('nip'=>$id))->row(); } function ubahdata($id){ $nip = $this->input->post('nip'); $nama = $this->input->post('nama'); $alamat = $this->input->post('alamat'); $tgllahir = $this->input->post('tgllahir'); $tunjangan = $this->input->post('tunjangan'); /*$agama = $this->input->post('agama'); $kddepartemen = $this->input->post('kddepartemen');*/ $data = array( 'nip' => $nip, 'nama' => $nama, 'alamat' => $alamat, 'tgllahir' => $tgllahir, 'tunjangan' => $tunjangan/*, 'agama' => $agama, 'kddepartemen' => $kddepartemen*/ ); $this->db->where('nip',$id); $this->db->update('guru',$data); } } ?>
Langkah terakhir tambahkan kode program ini pada Controller guru.php yang ada di webci/application/controllers/guru.php :
<?php class Guru extends CI_Controller{ function index(){ $this->load->model('mguru'); $data['isi'] = $this->mguru->getdata(); $this->load->view('vguru',$data); } function tambah() { if($this->input->post('submit')){ $this->load->model('mguru'); $this->mguru->simpandata(); //$this->do_upload(); redirect('guru/index'); } //$this->load->model('mguru'); //$data['barisdepartemen'] = $this->mguru->getdatadepartemen(); $this->load->view('vaddguru'); //,$data); } function hapus($id) { //$this->db->delete('guru',array('nip'=>$id)); $this->db->where('nip',$id); $this->db->delete('guru'); redirect('guru/index'); } function ubah($id) { if($this->input->post('submit')) { $this->load->model('mguru'); $this->mguru->ubahdata($id); //$this->do_upload(); redirect('guru/index'); } else { $this->load->model('mguru'); $data['baris'] = $this->mguru->selectdata($id); //$data['barisdepartemen'] = $this->mguru->getdatadepartemen(); $this->load->view('veditguru',$data); } } } ?>
Hasilnya :
Project selengkapnya menyusul :)
Semoga berguna.
Tidak ada komentar:
Posting Komentar