• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help with flow of code

#1
[eluser]megabyte[/eluser]
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.

Code:
function save_product(){

    // Validation rules
    $vrules['name']    = "required";
    $vrules['description'] = 'required';
    $vrules['price'] = 'required';
    $vrules['packaging'] = 'required';
    $this->validation->set_rules($vrules);    
    $fields['name'] = 'Name';
    $fields['description'] = 'Description';
    //$fields['userfile'] = 'Image';
    $fields['price'] = 'Price';
    $fields['packaging'] = 'Packaging';
    $this->validation->set_fields($fields);    
    // 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;
    exit;
    }
        
    $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
    if($this->id){
    $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');
    }

            
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.