[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'>
<?php foreach($trucks as $t): ?>
<p><?php echo $t->name; ?></p>
//"nopic" is the default name if user does not upload a picture to database.
//if image does not equal "nopic", then display image.
<?php if( ($t->image1) != 'nopic' ): ?>
<img src="<?php echo base_url('assets/images/').'/'.$t->image1; ?>" width="50" alt=""><br />
<?php endif; ?>
// if image does not equal "nopic", then display image.
<?php if( ($t->image2) != 'nopic' ): ?>
<img src="<?php echo base_url('assets/images/').'/'.$t->image2; ?>" width="30" alt="">
<?php endif; ?>
// if image does not equal "nopic", then display image.
<?php if( ($t->image3) != 'nopic' ): ?>
<img src="<?php echo base_url('assets/images/').'/'.$t->image3; ?>" width="30" alt="">
<?php endif; ?>
// if image does not equal "nopic", then display image.
<?php if( ($t->image4) != 'nopic' ): ?>
<img src="<?php echo base_url('assets/images/').'/'.$t->image4; ?>" width="30" alt="">
<?php endif; ?>
// if image does not equal "nopic", then display image.
<?php if( ($t->image5) != 'nopic' ): ?>
<img src="<?php echo base_url('assets/images/').'/'.$t->image5; ?>" width="30" alt="">
<?php endif; ?>
<?php endforeach; ?>
// show upload errors
<?php if(isset($error)){echo $error;}; ?>
// form to upload images
<form action="<?php echo base_url('upload'); ?>" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="text" name="name"><br />
<input type="file" name="file1"><br />
<input type="file" name="file2"><br />
<input type="file" name="file3"><br />
<input type="file" name="file4"><br />
<input type="file" name="file5"><br />
<input type="submit" name="submit" value="submit">
</form>
</div><!-- #Content -->