rendered paste body// Model function editProduct() { // Delete current image if box was checked if ($this->input->post('deleteimage') && $this->input->post('currentimage')) { $this->image->deleteImage('products',$this->input->post('currentimage')); } // Set our validation rules and custom delimiters (for display) for the form $this->validation->set_rules(array('pid' => 'trim|required', 'cid' => 'trim|required', 'name' => 'trim|required|min_length[5]|max_length[128]', 'description' => 'trim')); $this->validation->set_error_delimiters('<li>', '</li>'); // If form fails validation, reload data for view if ($this->validation->run() == FALSE) { $pid = (int) $this->input->post('pid'); ($pid) ? $data['pid'] = $pid : $data['pid'] = (int) $this->uri->segment(3); $data['product'] = $this->product->getProduct($data['pid']); // Check if product exists and load edit form or bounce to admin home if ($data['product']) { // Load category names and cids for select list $data['categories'] = $this->category->getallCategory(); $this->load->view('editproduct', $data); } else { $this->session->set_userdata('warningMessage', 'Product does not exist.'); redirect('/admin/'); } } else { // Load data with the information from our form $data = array('pid' => (int) $this->input->post('pid'), 'cid' => $this->input->post('cid'), 'name' => $this->input->post('name'), 'description' => $this->input->post('description'), 'image' => $this->image->addImage('products')); // If image upload failed, set back to current image, otherwise, delete old images $data['image'] === false ? $data['image'] = $this->input->post('currentimage') : $this->image->deleteImage('products',$this->input->post('currentimage')); // Surpress "no file selected for upload message" for edits if (empty($_FILES['userfile']['name'])) $this->session->set_userdata('warningMessage', ''); // Update product with data, set flash message and return to main admin page $this->product->updateProduct($data['pid'], $data); $this->session->set_userdata('statusMessage', 'Product <b>'.$data['name'].'</b> has been updated.'); redirect('/admin/listproducts/'.$data['cid']); } } // Model<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head> <title>Administration :: Edit Product: <?= $product->name ?></title> <link type="text/css" rel="stylesheet" media="all" href="/assets/css/style.css" /> </head> <body> <? $this->load->view('header'); ?> <h2>Edit Product: <?= $product->name ?></h2> <?= $this->validation->error_string ? '<div id="warningmessage"><b>There were problems updating the product:</b><ul>'.$this->validation->error_string.'</ul></div>' : ''; ?> <?= form_open_multipart('/admin/editproduct') ?> <fieldset id="productinfo"> <legend>Product Info</legend> <input type="hidden" name="pid" value="<?= $product->pid; ?>" /> <input type="hidden" name="currentimage" value="<?= $product->image; ?>" /> <label for="Product Name">Product Name:</label> <input type="text" name="name" value="<?= isset($this->validation->name) ? $this->validation->name : $product->name ?>" size="50" /> <br> <label for="Product Category">Product Category:</label> <select name="cid"> <? foreach ($categories->result() as $categoryoption): ?> <option value="<?= $categoryoption->cid ?>"<?= $this->validation->set_select('cid', $categoryoption->cid) ?>><?= $categoryoption->name ?></option> <? endforeach ?> </select> <br> <label for="Product Description">Product Description:</label> <textarea name="description" rows="5" cols="50"><?= isset($this->validation->description) ? $this->validation->description : $product->description ?></textarea> </fieldset> <fieldset id="productimage"> <legend>Product Image</legend> <label for="Product Image">Product Image:</label> <input type="file" name="userfile" value="<?= isset($this->validation->image) ? $this->validation->image : $product->image ?>" size="35" /> <? if ($product->image) : ?> <br> <label for="Delete Image">Delete Image?</label> <input type="checkbox" name="deleteimage" value="1" <?= $this->validation->set_checkbox('deleteimage', '1'); ?> /> Check to delete current image. No need to check if uploading a new image. <? endif ?> <p style="text-align: center;"><?= $product->image ? makeThumbnailPopup($product->image, '/assets/images/products/', $product->name) : 'No image is associated with this product.'?></p> </fieldset> <p><?= form_submit('submit', 'Edit Product') ?></p> <?= form_close() ?> <? $this->load->view('footer'); ?> </body></html>