Professional CodeIgniter

(singke) #1

Chapter 6: Creating a Dashboard


172


$config[‘upload_path’] = ‘./images/’;
$config[‘allowed_types’] = ‘gif|jpg|png’;
$config[‘max_size’] = ‘200’;
$config[‘remove_spaces’] = true;
$config[‘overwrite’] = false;
$config[‘max_width’] = ‘0’;
$config[‘max_height’] = ‘0’;
$this- > load- > library(‘upload’, $config);

After you ’ ve configured and loaded the upload library, use the do_upload() method to upload
whatever files are designated in the form fields named image and thumbnail. Once the files have been
uploaded, add their path information into the $data array you ’ ve already started at the top of the
function.

if(!$this- > upload- > do_upload(‘image’)){
$this- > upload- > display_errors();
exit();
}
$image = $this- > upload- > data();

if ($image[‘file_name’]){
$data[‘image’] = “/images/”.$image[‘file_name’];
}

if(!$this- > upload- > do_upload(‘thumbnail’)){
$this- > upload- > display_errors();
exit();
}
$thumb = $this- > upload- > data();

if ($thumb[‘file_name’]){
$data[‘thumbnail’] = “/images/”.$thumb[‘file_name’];
}

Now, use the $this- > db- > insert() method to add the data record to the products table.

$this- > db- > insert(‘products’, $data);
}

All that ’ s left to do now is to create a simple admin_product_create view, which is essentially a multi -
part form. You will need to use the form_open_multipart() function to support file uploads.

< h1 > < ?php echo $title;? > < /h1 >

< ?php
echo form_open_multipart(‘admin/products/create’);

Next, step through the usual list of suspects to print out dropdowns and text fields for category_id,
name, shortdesc, and longdesc.
Free download pdf