Advertisement
jamboljack

Restaurant

Jun 17th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. Controller :
  2.  
  3.     public function savedata() {          
  4.         $this->form_validation->set_rules('namarestaurant', '<b>Nama Restaurant</b>','trim|required|min_length[5]|max_length[50]|is_unique[restaurant.nama_restaurant]');      
  5.         $this->form_validation->set_rules('deskripsi', '<b>Deskripsi</b>','trim|required|min_length[10]');
  6.                                
  7.         if ($this->form_validation->run() == FALSE) {
  8.             $this->template_admin->display('a/addrestaurant');
  9.         } else {
  10.             // Buat nampung data image yg berhasil diupload
  11.             $upload_data = array();
  12.             // Default konfigurasi, biar nggak perlu nulis ulang :v
  13.             $upload_conf = array(
  14.                                 'upload_path'   => './restoran/',
  15.                                 'allowed_types' => 'gif|jpg|png',
  16.                                 'overwrite'     => TRUE,
  17.                         );
  18.  
  19.             // File 1
  20.             if (isset($_FILES['userfile'])) {
  21.                 $upload_conf['overwrite'] = url_title($this->input->post('userfile'));
  22.                 // Load upload lib
  23.                 $this->load->library('upload', $upload_conf);
  24.                      // Pastiin uploadnya berhasil
  25.                     if ($this->upload->do_upload('userfile')) {
  26.                         // ngedapetin data hasil upload
  27.                         $upload_data['userfile'] = $this->upload->data();
  28.                     }
  29.             } else {
  30.                 $upload_data['userfile'] = "";
  31.             }
  32.  
  33.             // File 2
  34.             if (isset($_FILES['userfile1'])) {
  35.                 $upload_conf['overwrite'] = url_title($this->input->post('userfile1'));
  36.                 // Load upload lib
  37.                 $this->load->library('upload', $upload_conf);
  38.                      // Pastiin uploadnya berhasil
  39.                     if ($this->upload->do_upload('userfile1')) {
  40.                         // ngedapetin data hasil upload
  41.                         $upload_data['userfile1'] = $this->upload->data();
  42.                     }
  43.             } else {
  44.                 $upload_data['userfile1'] = "";
  45.             }
  46.                        
  47.             // mastiin kalo data hasil uploadnya udah siap
  48.             if (!empty($upload_data)) {
  49.                 // Default image lib config. lagi, biar nggak perlu nulis ulang.
  50.                 $imglib_conf = array(
  51.                     'image_library'  => 'gd2',
  52.                     'maintain_ratio' => TRUE,
  53.                     'width'          => 600,
  54.                     'height'         => 360,
  55.                     );
  56.  
  57.                     // olah image hasil upload,
  58.                     foreach ($upload_data as $id => $img_data) {
  59.                         // ambil source image dari $upload_data
  60.                         $imglib_conf['source_image'] = $img_data['full_path'];
  61.                         // Load image_lib class
  62.                         $this->load->library('image_lib', $imglib_conf);
  63.                         // resizing
  64.                         $this->image_lib->resize();
  65.                     }
  66.             }
  67.  
  68.             $data = array(
  69.                     'nama'      => ucwords($this->input->post('namarestaurant')),
  70.                     'deskripsi' => $this->input->post('deskripsi'),
  71.                     'foto1'     => '',
  72.                     'foto2'     => ''
  73.                     );
  74.  
  75.             if (isset($_FILES['userfile'])) {
  76.                 $data['foto1'] = $upload_data['userfile']['file_name'];
  77.             }
  78.  
  79.             if (isset($_FILES['userfile1'])) {
  80.                 $data['foto2'] = $upload_data['userfile1']['file_name'];
  81.             }
  82.                        
  83.             $this->restaurant_model->insert_data($data);
  84.             redirect(base_url().'a/restaurant');  
  85.         }
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement