Rabu, 03 Juni 2020

CI Multi Upload Image





  public function save()  {
        $post = $this->input->post();
       // $this->id_lpi = uniqid();

$this->nama_lpi = $post["nama_lpi"];
$this->deskripsi = $post["deskripsi"];
$this->fasilitas = $post["fasilitas"];
$this->alamat = $post["alamat"];
$this->status = $post["status"];

$this->latitude= $post["latitude"];
$this->longitude= $post["longitude"];
$this->akreditasi= $post["akreditasi"];
$this->keterangan = $post["keterangan"];

$this->gambar =$_FILES["gambar"]["name"];
$this->gambar2 =$_FILES["gambar2"]["name"];
$this->gambar3 =$_FILES["gambar3"]["name"];
$this->gambar4 =$_FILES["gambar4"]["name"];
$this->gambar5 =$_FILES["gambar5"]["name"];
//$this->_uploadImage();



          $config['upload_path'] = './upload/';
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '5000';
 
          $this->load->library('upload',$config);
          if($this->upload->do_upload('gambar')){
            $uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
            $this->gambar = $filename;
          }
  //=========================================


          $config['upload_path'] = './upload/';
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '5000';
 
          $this->load->library('upload',$config);
          if($this->upload->do_upload('gambar2')){
            $uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
            $this->gambar2 = $filename;
          }
  //=========================================


          $config['upload_path'] = './upload/';
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '5000';
 
          $this->load->library('upload',$config);
          if($this->upload->do_upload('gambar3')){
            $uploadData = $this->upload->data();
            $filename = $uploadData['file_name'];
            $this->gambar3 = $filename;
          }
  //=========================================


          $config['upload_path'] = './upload/';
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '5000';
          $this->load->library('upload',$config);
 
          if($this->upload->do_upload('gambar4')){
            $uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
            $this->gambar4 = $filename;
          }
  //=========================================


          $config['upload_path'] = './upload/';
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '5000';
          $this->load->library('upload',$config);
 
          if($this->upload->do_upload('gambar5')){
            $uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
            $this->gambar5 = $filename;
          }
  //=========================================

$this->db->insert($this->_table, $this);


    }


Dan ini rumus updatenya:

  public function update() {
        $post = $this->input->post();
$this->id_lpi = $post["id_lpi"];
$this->nama_lpi = $post["nama_lpi"];
$this->deskripsi = $post["deskripsi"];
$this->fasilitas = $post["fasilitas"];
$this->alamat = $post["alamat"];
$this->status = $post["status"];

$this->latitude= $post["latitude"];
$this->longitude= $post["longitude"];
$this->akreditasi= $post["akreditasi"];
$this->keterangan = $post["keterangan"];


$this->gambar = $post["old_gambar"];
if (!empty($_FILES["gambar"]["name"])) {
            $config['upload_path'] = './upload/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000';
 
  $this->load->library('upload',$config);
  if($this->upload->do_upload('gambar')){
$uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
$this->gambar = $filename;
  }
        }
//--------------------------------------
$this->gambar2 = $post["old_gambar2"];
if (!empty($_FILES["gambar2"]["name"])) {
            $config['upload_path'] = './upload/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000';
 
  $this->load->library('upload',$config);
  if($this->upload->do_upload('gambar2')){
$uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
$this->gambar2 = $filename;
  }
        }
//--------------------------------------
$this->gambar3 = $post["old_gambar3"];
if (!empty($_FILES["gambar3"]["name"])) {
            $config['upload_path'] = './upload/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000';
 
  $this->load->library('upload',$config);
  if($this->upload->do_upload('gambar3')){
$uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
$this->gambar3 = $filename;
  }
        }
//--------------------------------------
$this->gambar4 = $post["old_gambar4"];
if (!empty($_FILES["gambar4"]["name"])) {
            $config['upload_path'] = './upload/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000';
 
  $this->load->library('upload',$config);
  if($this->upload->do_upload('gambar4')){
$uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
$this->gambar4 = $filename;
  }
        }
//--------------------------------------
$this->gambar5 = $post["old_gambar5"];
if (!empty($_FILES["gambar5"]["name"])) {
            $config['upload_path'] = './upload/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000';
 
  $this->load->library('upload',$config);
  if($this->upload->do_upload('gambar5')){
$uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
$this->gambar5 = $filename;
  }
        }
//--------------------------------------

        $this->db->update($this->_table, $this, array('id_lpi' => $post['id_lpi']));
    }


Dan ini di controllernya:

 public function add(){ 
        $lpi = $this->lpi_model;
        $validation = $this->form_validation;
        $validation->set_rules($lpi->rules());

        if ($validation->run()) {
            $lpi->save();
            $this->session->set_flashdata('success', 'Berhasil Disimpan');
redirect(site_url('admin/lpis'));
        }
else{
$this->load->view("admin/lpi/new_form",null);
}
    }

    public function edit($id = null){
        if (!isset($id)) redirect('admin/lpis');
       
        $lpi = $this->lpi_model;
        $validation = $this->form_validation;
        $validation->set_rules($lpi->rules());

        if ($validation->run()) {
            $lpi->update();
            $this->session->set_flashdata('success', 'Berhasil diubah');
redirect(site_url('admin/lpis'));
        }
else{
        $data["lpi"] = $lpi->getById($id);
        if (!$data["lpi"]) show_404();
        
        $this->load->view("admin/lpi/edit_form", $data);
}//else
    }

Tidak ada komentar:

Posting Komentar