Professional CodeIgniter

(singke) #1

Chapter 6: Creating a Dashboard


175


In this case, you only have to create one new MProducts function, one called updateProduct(). It is
very much like the addProduct() function except that it performs a SQL update instead of a SQL insert.


function updateProduct(){
$data = array(
‘name’ = > $_POST[‘name’],
‘shortdesc’ = > $_POST[‘shortdesc’],
‘longdesc’ = > $_POST[‘longdesc’],
‘status’ = > $_POST[‘status’],
‘grouping’ = > $_POST[‘grouping’],
‘category_id’ = > $_POST[‘category_id’],
‘featured’ = > $_POST[‘featured’],
‘price’ = > $_POST[‘price’]

);
$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);

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’];
}
$this- > db- > where(‘id’, $_POST[‘id’]);
$this- > db- > update(‘products’, $data);
}

Again, please make sure that the folder designated by upload_path is writable!

Finally, here ’ s the admin_product_edit view, which in many regards is the same form as adminproduct
create, except it posts to a different controller function (admin/products/edit), contains a hidden field
with the product ID (needed by the updateProduct() function!), and loads values from the database
for that particular product into the form fields.

Free download pdf