File pertemuan minggu lalu bisa didownload disini (pertemuan 10)
untuk membuat sebuah combobox / dropdown berdasarkan relasi tabel pada database bisa menggunakan helper di CodeIgniter
Cara :
Buat sebuah file php dalam folder ( application/helper) dengan nama my_helper.php
<?php function cmb_dinamis($name,$table,$field,$pk,$selected){ $ci = get_instance(); $cmb = "<select name='$name' class='form-control selectan'>"; $cmb .= "<option value=''>PILIH JURUSAN</option>"; $data = $ci->db->get($table)->result(); foreach ($data as $d){ $cmb .="<option value='".$d->$pk."'"; $cmb .= $selected==$d->$pk?" selected='selected'":''; $cmb .=">". strtoupper($d->$field)."</option>"; } $cmb .="</select>"; return $cmb; }
Kemudian … . . .
tambahkan fungsi baru di dalam file Biodata.php (application/controller)
private function ayok_upload(){ $this->load->library('upload'); $nmfile = 'gbr_'.time(); //nama file saya beri nama langsung dan diikuti fungsi time $config['upload_path'] = './gambar/'; //path folder $config['allowed_types'] = 'JPG|JPEG|PNG|BMP|png'; //type yang dapat diakses bisa anda sesuaikan $config['max_size'] = '20000'; //maksimum besar file 20mb $config['file_name'] = $nmfile; //nama yang terupload nantinya $this->upload->initialize($config); }
Selanjutnya Edit fungtion create_action menjadi
public function create_action() { $this->_rules(); $this->ayok_upload(); if ($this->form_validation->run() == FALSE) { $this->create(); } else { if(!empty($_FILES['foto']['name'])) { // empty if ($this->upload->do_upload('foto')) { $gbr = $this->upload->data(); $data = array( 'nim' => $this->input->post('nim',TRUE), 'nama' => $this->input->post('nama',TRUE), 'jk' => $this->input->post('jk',TRUE), 'tempat_lahir' => $this->input->post('tempat_lahir',TRUE), 'tgl_lahir' => $this->input->post('tgl_lahir',TRUE), 'alamat' => $this->input->post('alamat',TRUE), 'jur' => $this->input->post('jur',TRUE), 'no_hp' => $this->input->post('no_hp',TRUE), 'foto' => $gbr['file_name'], ); $this->Biodata_model->insert($data); $this->session->set_flashdata('message', 'Create Record Success'); redirect(site_url('biodata')); }else{ $this->session->set_flashdata('message_form', '<div id="alert" class="alert alert-danger"><i class="glyphicon glyphicon-ok"></i> GAGAL<br/>Silahkan Coba lagi ! ! !<br/>'.$this->upload->display_errors().'</div>'); $this->create(); } }else{ // empty $data = array( 'nim' => $this->input->post('nim',TRUE), 'nama' => $this->input->post('nama',TRUE), 'jk' => $this->input->post('jk',TRUE), 'tempat_lahir' => $this->input->post('tempat_lahir',TRUE), 'tgl_lahir' => $this->input->post('tgl_lahir',TRUE), 'alamat' => $this->input->post('alamat',TRUE), 'jur' => $this->input->post('jur',TRUE), 'no_hp' => $this->input->post('no_hp',TRUE), 'foto' => '', ); $this->Biodata_model->insert($data); $this->session->set_flashdata('message', 'Create Record Success'); redirect(site_url('biodata')); } } }
Kemudian function update_action menjadi
public function update_action() { $this->_rules(); $this->ayok_upload(); if ($this->form_validation->run() == FALSE) { $this->update($this->input->post('id', TRUE)); } else { if(!empty($_FILES['foto']['name'])) { // empty if ($this->upload->do_upload('foto')) { $gbr = $this->upload->data(); $hapus = $this->Biodata_model->get_by_id($this->input->post('id', TRUE)); if(file_exists('gambar/'.$hapus->foto)) unlink('gambar/'.$hapus->foto); $data = array( 'nim' => $this->input->post('nim',TRUE), 'nama' => $this->input->post('nama',TRUE), 'jk' => $this->input->post('jk',TRUE), 'tempat_lahir' => $this->input->post('tempat_lahir',TRUE), 'tgl_lahir' => $this->input->post('tgl_lahir',TRUE), 'alamat' => $this->input->post('alamat',TRUE), 'jur' => $this->input->post('jur',TRUE), 'no_hp' => $this->input->post('no_hp',TRUE), 'foto' => $gbr['file_name'], ); $this->Biodata_model->update($this->input->post('id', TRUE), $data); $this->session->set_flashdata('message', 'Update Record Success'); redirect(site_url('biodata')); }else{ $this->session->set_flashdata('message_form', '<div id="alert" class="alert alert-danger"><i class="glyphicon glyphicon-ok"></i> GAGAL<br/>Silahkan Coba lagi ! ! !<br/>'.$this->upload->display_errors().'</div>'); $this->update($this->input->post('id', TRUE)); } }else{ // empty $data = array( 'nim' => $this->input->post('nim',TRUE), 'nama' => $this->input->post('nama',TRUE), 'jk' => $this->input->post('jk',TRUE), 'tempat_lahir' => $this->input->post('tempat_lahir',TRUE), 'tgl_lahir' => $this->input->post('tgl_lahir',TRUE), 'alamat' => $this->input->post('alamat',TRUE), 'jur' => $this->input->post('jur',TRUE), 'no_hp' => $this->input->post('no_hp',TRUE), ); $this->Biodata_model->update($this->input->post('id', TRUE), $data); $this->session->set_flashdata('message', 'Update Record Success'); redirect(site_url('biodata')); } } }
function delete
public function delete($id) { $row = $this->Biodata_model->get_by_id($id); if ($row) { $this->Biodata_model->delete($id); $this->session->set_flashdata('message', 'Delete Record Success'); redirect(site_url('biodata')); } else { $this->session->set_flashdata('message', 'Record Not Found'); redirect(site_url('biodata')); } }