Welcome Guest, Not a member yet? Register   Sign In
feedback on code (upload library)
#1

[eluser]brian88[/eluser]
Here Im allowing a user to upload a max of 5 images and stores the image name in a database. Im most likely going to bump it up to 10 images. As you can see I have a lot of repeated code and its going to double when I go from uploading 5 images to 10 images.

Everything works, but Im looking for feedback to make this code shorter and more simple for the controller and the view.


heres what the database looks like...... if no picture is uploaded then the default is "nopic"

id | name | image1 | image2 | image3 | image4 | image5
----------------------------------------------------------------------------------------------------------
1 | Dodge | dodge/pic1.png | dodge/pic2.png | dodge/pic3.png | dodge/pic4.png | nopic
2 | Ford | ford/pic1.png | ford/pic2.png | nopic | nopic | nopic
*/

controller
Code:
function index() {
   // get all trucks from database
   $data['trucks'] = $this->main_mod->getTrucks();

   // if user presses submit button
  if($this->input->post()){

   // get the name of the truck
   $db_data['name'] = $this->input->post('name');

   // format the truck name to be a folder name
   $folder = str_replace( " ", "", strtolower($this->input->post('name')) );

   // make a folder based off the name of the truck
   mkdir('assets/images/'.$folder, 0777);

   // config for file uploads
   $config['upload_path'] = FCPATH . 'assets/images/' . $folder;
   $config['allowed_types'] = 'gif|jpg|png|jpeg';
   $config['max_size'] = '0'; //1000 = 1MB
   $config['max_width']  = '0';
   $config['max_height']  = '0';
   $this->load->library('upload', $config);

   // check to see if user selected a file 1
   if ( !empty($_FILES['file1']['name']) ){
    // upload file 1
    if($this->upload->do_upload('file1')){
     $upload_data = $this->upload->data();
     $db_data['image1'] = $folder.'/'.$upload_data['file_name'];
    }else{
     echo $this->upload->display_errors();
    }
   }

   // check to see if user selected a file 2
   if ( !empty($_FILES['file2']['name']) ){
    // upload file 2
    if($this->upload->do_upload('file2')){
     $upload_data = $this->upload->data();
     $db_data['image2'] = $folder.'/'.$upload_data['file_name'];
    }else{
     echo $this->upload->display_errors();
    }
   }

   // check to see if user selected a file 3
   if ( !empty($_FILES['file3']['name']) ){
    // upload file 3
    if($this->upload->do_upload('file3')){
     $upload_data = $this->upload->data();
     $db_data['image3'] = $folder.'/'.$upload_data['file_name'];
    
    }else{
     echo $this->upload->display_errors();
    }
   }

   // check to see if user selected a file 4
   if ( !empty($_FILES['file4']['name']) ){
    // upload file 4
    if($this->upload->do_upload('file4')){
     $upload_data = $this->upload->data();
     $data['image4'] = $folder.'/'.$upload_data['file_name'];
    }else{
     echo $this->upload->display_errors();
    }
   }

   // check to see if user selected a file 5
   if ( !empty($_FILES['file5']['name']) ){
    // upload file 5
    if($this->upload->do_upload('file5')){
     $upload_data = $this->upload->data();
     $db_data['image5'] = $folder.'/'.$upload_data['file_name'];
    }else{
     echo $this->upload->display_errors();
    }
   }

   // add to database
   $this->main_mod->addTruck($db_data);

   redirect('/');
  }else{
   // views
   $data['content'] = 'main/post';
   $this->load->view('templates/main', $data);
  }
  
} // end function

view
Code:
<div id='content'>

  &lt;?php foreach($trucks as $t): ?&gt;

   <p>&lt;?php echo $t->name; ?&gt;</p>

  //"nopic" is the default name if user does not upload a picture to database.

  //if image does not equal "nopic", then display image.
  &lt;?php if( ($t->image1) != 'nopic' ): ?&gt;

   <img src="&lt;?php echo base_url('assets/images/').'/'.$t-&gt;image1; ?&gt;" width="50" alt=""><br />
  &lt;?php endif; ?&gt;

  // if image does not equal "nopic", then display image.
  &lt;?php if( ($t->image2) != 'nopic' ): ?&gt;

   <img src="&lt;?php echo base_url('assets/images/').'/'.$t-&gt;image2; ?&gt;" width="30" alt="">
  &lt;?php endif; ?&gt;

  // if image does not equal "nopic", then display image.
  &lt;?php if( ($t->image3) != 'nopic' ): ?&gt;

   <img src="&lt;?php echo base_url('assets/images/').'/'.$t-&gt;image3; ?&gt;" width="30" alt="">
  &lt;?php endif; ?&gt;

  // if image does not equal "nopic", then display image.
  &lt;?php if( ($t->image4) != 'nopic' ): ?&gt;

   <img src="&lt;?php echo base_url('assets/images/').'/'.$t-&gt;image4; ?&gt;" width="30" alt="">
  &lt;?php endif; ?&gt;

  // if image does not equal "nopic", then display image.
  &lt;?php if( ($t->image5) != 'nopic' ): ?&gt;

   <img src="&lt;?php echo base_url('assets/images/').'/'.$t-&gt;image5; ?&gt;" width="30" alt="">
  &lt;?php endif; ?&gt;

  &lt;?php endforeach; ?&gt;


// show upload errors
&lt;?php if(isset($error)){echo $error;}; ?&gt;

// form to upload images
   &lt;form action="&lt;?php echo base_url('upload'); ?&gt;" method="post" accept-charset="utf-8" enctype="multipart/form-data"&gt;
    &lt;input type="text" name="name"&gt;&lt;br />
    &lt;input type="file" name="file1"&gt;&lt;br />
    &lt;input type="file" name="file2"&gt;&lt;br />
    &lt;input type="file" name="file3"&gt;&lt;br />
    &lt;input type="file" name="file4"&gt;&lt;br />
    &lt;input type="file" name="file5"&gt;&lt;br />
    &lt;input type="submit" name="submit" value="submit"&gt;
   &lt;/form&gt;

  </div>&lt;!-- #Content --&gt;


Messages In This Thread
feedback on code (upload library) - by El Forum - 05-10-2012, 08:53 AM
feedback on code (upload library) - by El Forum - 05-15-2012, 06:24 AM
feedback on code (upload library) - by El Forum - 05-15-2012, 06:46 AM
feedback on code (upload library) - by El Forum - 05-15-2012, 06:50 AM
feedback on code (upload library) - by El Forum - 05-15-2012, 08:44 AM
feedback on code (upload library) - by El Forum - 05-16-2012, 12:23 PM
feedback on code (upload library) - by El Forum - 05-16-2012, 12:50 PM
feedback on code (upload library) - by El Forum - 05-16-2012, 11:11 PM
feedback on code (upload library) - by El Forum - 05-17-2012, 06:52 AM
feedback on code (upload library) - by El Forum - 05-17-2012, 09:47 AM



Theme © iAndrew 2016 - Forum software by © MyBB