Faber Nainggolan Blog's

Artikel, Tutorial Pemrograman, Tutorial database, PHP dan Framework

LightBlog

Pada artikel sebelumnya kita telah membuat upload file dan lanjutnya adalah kita akan modifikasi dan menghapus tersebut. Oke langsung kita m...

Memulai Project CodeIgniter : Part 9 Upload File bagian 2

Pada artikel sebelumnya kita telah membuat upload file dan lanjutnya adalah kita akan modifikasi dan menghapus tersebut. Oke langsung kita mulai
Langkah awal kita tambahkan fungsi untuk menangani edit dan hapus upload nya buat buatkan dibawah fungsi terakhir pada controller Upload.php

//fungsi untuk menangani pemanggilan aksi edit ketika form upload di submit
    public function aksi_edit(){
        $kode     = addslashes($this->input->post('kode'));
        $nama     = addslashes($this->input->post('nama'));
        $keterangan    = addslashes($this->input->post('keterangan'));
        $filelama = $this->input->post('gambarold');
 
        //configurasi upload
        $path   = "./assets/uploads/";
        $nmfile = "file_".time(); //nama file saya beri nama langsung dan diikuti fungsi time
        $config['upload_path'] = $path; //path folder
        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
        $config['max_size'] = '2048'; //maksimum besar file 2M
        $config['max_width']  = '1288'; //lebar maksimum 1288 px
        $config['max_height']  = '768'; //tinggi maksimu 768 px
        $config['file_name'] = $nmfile; //nama yang terupload nantinya
        $this->upload->initialize($config);

        if ($this->upload->do_upload('gambar')){
                $gbr = $this->upload->data(); //gambar diupload
                $data = array(
                  'nm_gbr'  => $gbr['file_name'],
                  'tipe_gbr' => $gbr['file_type'],
                  'ket_gbr'=> $keterangan
                );
                @unlink($path.$filelama);//menghapus gambar lama, variabel dibawa dari form
                $this->mupload->getupdate($kode,$data); //model update data
                $this->session->set_flashdata("pesan", "Gambar berhasil diperbaharui"); //pesan yang tampil setelah berhasil di update
                redirect('upload');
        }else{ 
            $info = array(
              'aket'=>$keterangan,
              'judul' => 'Tambah Gambar',
              'aksi' => 'aksi_edit'
            );
            $err = $this->upload->display_errors(); //error upload akan dimunculkan pada pesan

            $this->session->set_flashdata("pesan", "Data Gagal di update <br>".$err);

            //ketika error terjadi halaman akan tetap di form barang
            $this->load->view('header',$info); 
            $this->load->view('upload/vformupload',$info);
            $this->load->view('footer',$info); 
        }
    }
 
    //fungsi untuk menangani apabila hapus gambar dan hapus file upload
    public function hapus(){
        $id = $this->input->get('id'); //variabel id dari url
        $path = "./assets/uploads/";
        $row = $this->mupload->getbyid($id);
        $gbrlama = $row->nm_gbr;
        @unlink($path.$gbrlama);//menghapus gambar lama, variabel dibawa dari form
        $this->mupload->getdelete($id);
        $this->session->set_flashdata("pesan", "File berhasil dihapus");
        redirect('upload');
    }


Untuk Models Mbarang.php nya buat jadi seperti ini
<?php
class Mbarang extends CI_Model {
    var $tabel = 'tb_barang'; //variabel tabel

    function __construct() {
        parent::__construct();
    }

    function getallbarang($batas =null,$offset=null,$key=null) {
        $this->db->from($this->tabel);
        $this->db->order_by('kode_brg','DESC');
        if($batas != null){
            $this->db->limit($batas,$offset);
        }
        if ($key != null) {
           $this->db->or_like($key);
        }
        $query = $this->db->get();

        //cek apakah ada barang
        if ($query->num_rows() > 0) {
            return $query->result();
        }
    }

    function getbarangbyid($id) {
        $this->db->from($this->tabel);
        $this->db->where('kode_brg', $id);

        $query = $this->db->get();
        if ($query->num_rows() == 1) {
            return $query->row();
        }
    }

    function getinsert($data){
        $this->db->insert($this->tabel, $data);
        return TRUE;
    }

    function getupdate($id,$data) {
        $this->db->where('kode_brg', $id);
        $this->db->update($this->tabel, $data);
        return TRUE;
    }

    function delbarang($id) {
        $this->db->where('kode_brg', $id);
        $this->db->delete($this->tabel);
        if ($this->db->affected_rows() == 1) {
            return TRUE;
        }
        return FALSE;
    }

    function countbarang(){
        $query = $this->db->get($this->tabel)->num_rows();
        return $query;
    }

    function  countbarangsearch($orlike) {
        $this->db->or_like($orlike);
        $query = $this->db->get($this->tabel);

        return $query->num_rows();
    }
}
?>

untuk views nya pada artikel sebelumnya sudah saya includekan langsung.

nah coba jalankan di browser apakah sudah berhasil, jika diikuti dengan baik saya fikir untuk proses edit dan hapusnya pasti berhasil
Terimakasih semoga tutorial ini bermanfaat

Sumber Referensi
https://www.codeigniter.com/user_guide/libraries/file_uploading.html

0 comments: