CodeIgniter Forums
Need some help with flow of code - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Need some help with flow of code (/thread-6026.html)

Need some help with flow of code - El Forum - 02-12-2008

Just wondering if I'm using exit correctly, or if I should be coding this a better way. Any other comments on how to make it better much appreciated. It does work, so there are no errors. I'm just feeling like it could be written better. and I'd like to learn how.

function save_product(){

    // Validation rules
    $vrules['name']    = "required";
    $vrules['description'] = 'required';
    $vrules['price'] = 'required';
    $vrules['packaging'] = 'required';
    $fields['name'] = 'Name';
    $fields['description'] = 'Description';
    //$fields['userfile'] = 'Image';
    $fields['price'] = 'Price';
    $fields['packaging'] = 'Packaging';
    // Set the error delims to a nice styled red hint under the fields
    //$this->validation->set_error_delimiters('<p class="hint error"><span>', '</span></p>');
    $this->validation->set_error_delimiters('', '<br />');          
    // Create array for database fields & data
    $data = array();
    $data['name'] = $this->input->post('name');
    $data['description'] = $this->input->post('description');
    $data['price'] = $this->input->post('price');
    $data['packaging'] = $this->input->post('packaging');
    // If validation fails, print errors
    if ($this->validation->run() == FALSE OR $this->upload_image($data['name']) == FALSE){
    $msg = $this->validation->error_string;
    $msg .= "<br />".$this->upload->display_errors();
    echo $msg;
    $image = array('upload_data' => $this->upload->data());
    //resize the image
    $this->resize_image('GD2', $image['upload_data']['full_path'], 100, 100);
    $image_data['image_name'] = $image['upload_data']['file_name'];
    // We have an id so we edit
    $this->base_model->edit('photos', 'product_id', $image_data, $this->id);
    $this->base_model->edit('products', 'id', $data, $this->id);
    $msg = 'Changes Saved';
    setFlashMsg($msg, 'success');
    // No id, adding new record
    else {
    $this->base_model->add('products', $data);
    $image_data['product_id'] = $this->db->insert_id();
    $this->base_model->add('photos', $image_data);                                        
    /* add this product under the specified categories */
    $query = $this->db->query("
       INSERT INTO products_categories (category_id, product_id)
       VALUES ('{$this->sub_id}', '$product_id')
   // Add/Edit was Successful
    $msg = 'Product Added';
    setFlashMsg($msg, 'success');