Kali ini kita gunakan Helper Form untuk membuat halaman tambah. Buatlah file view vaddguru.php pada direktori webci/application/views/vaddguru.php :
<?php echo form_open_multipart('guru/tambah',array('method'=>'post')) ?> <table> <tr><td>NIP</td><td><?php echo form_input(array('name'=>'nip','size'=>'20','maxlength'=>'20')); ?></td></tr> <tr><td>Nama</td><td><?php echo form_input('nama'); ?></td></tr> <tr><td>Alamat</td><td><?php echo form_input('alamat'); ?></td></tr> <tr><td>Tgl Lahir</td><td><?php echo form_input('tgllahir'); ?></td></tr> <tr><td>Tunjangan</td><td><?php echo form_input('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,''); */ ?> </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,''); */ ?> </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 berikut ini pada model mguru.php yang terletak 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); } } ?>
Terakhir tambahkan kode program ini pada Controller guru.php 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); } } ?>
Hasilnya :
Project selengkapnya menyusul.
Semoga bermanfaat.
Tidak ada komentar:
Posting Komentar