application/controllers/SPharmacy.php
/*
    * If the validation is not valid, then it redirects to the edit product page 
    * If the validation is successfully then it updates the data into the database 
    * and it stores the operation message into the session flashdata and display on the manage product page
    */
If the validation is not valid, then it redirects to the edit product page
If the validation is successfully then it updates the data into the database
and it stores the operation message into the session flashdata and display on the manage product page
If the validation is not valid then it redirects to the edit product page If the validation is successfully then it updates the data into the database and it stores the operation message into the session flashdata and display on the manage product page
public function update($product_id)
	{      
        if(!in_array('updateProduct', $this->permission)) {
            redirect('dashboard', 'refresh');
        }
        if(!$product_id) {
            redirect('dashboard', 'refresh');
        }
        $this->form_validation->set_rules('product_name', 'Product name', 'trim|required');
        $this->form_validation->set_rules('sku', 'SKU', 'trim|required');
        $this->form_validation->set_rules('price', 'Price', 'trim|required');
        $this->form_validation->set_rules('qty', 'Qty', 'trim|required');
        $this->form_validation->set_rules('store', 'Store', 'trim|required');
        $this->form_validation->set_rules('availability', 'Availability', 'trim|required');
        if ($this->form_validation->run() == TRUE) {
            $data = array(
                'name' => $this->input->post('product_name'),
                'sku' => $this->input->post('sku'),
                'cost_price' => $this->input->post('cost_price'),
                'price' => $this->input->post('price'),
                'qty' => $this->input->post('qty'),
                'description' => $this->input->post('description'),
                'attribute_value_id' => json_encode($this->input->post('attributes_value_id')),
                'brand_id' => json_encode($this->input->post('brands')),
                'category_id' => json_encode($this->input->post('category')),
                'store_id' => $this->input->post('store'),
                'availability' => $this->input->post('availability'),
            );
            if($_FILES['product_image']['size'] > 0) {
                $upload_image = $this->upload_image();
                $upload_image = array('image' => $upload_image);
                $this->model_products->update($upload_image, $product_id);
            }
            $update = $this->model_products->update($data, $product_id);
            if($update == true) {
                $this->session->set_flashdata('success', 'Successfully updated');
                redirect('products/', 'refresh');
            }
            else {
                $this->session->set_flashdata('errors', 'Error occurred!!');
                redirect('products/update/'.$product_id, 'refresh');
            }
        }
        else {
            $attribute_data = $this->model_attributes->getActiveAttributeData();
            $attributes_final_data = array();
            foreach ($attribute_data as $k => $v) {
                $attributes_final_data[$k]['attribute_data'] = $v;
                $value = $this->model_attributes->getAttributeValueData($v['id']);
                $attributes_final_data[$k]['attribute_value'] = $value;
            }
            $this->data['attributes'] = $attributes_final_data;
            $this->data['brands'] = $this->model_brands->getActiveBrands();         
            $this->data['category'] = $this->model_category->getActiveCategroy();           
            $this->data['stores'] = $this->model_stores->getActiveStore();          
            $product_data = $this->model_products->getProductData($product_id);
            $this->data['product_data'] = $product_data;
            $this->render_template('products/edit', $this->data); 
        }   
	}
public function update ( $ product_id ) { if ( ! in_array ( 'updateProduct' , $ this -> permission ) ) { redirect ( 'dashboard' , 'refresh' ) ; } if ( ! $ product_id ) { redirect ( 'dashboard' , 'refresh' ) ; } $ this -> form_validation -> set_rules ( 'product_name' , 'Product name' , 'trim|required' ) ; $ this -> form_validation -> set_rules ( 'sku' , 'SKU' , 'trim|required' ) ; $ this -> form_validation -> set_rules ( 'price' , 'Price' , 'trim|required' ) ; $ this -> form_validation -> set_rules ( 'qty' , 'Qty' , 'trim|required' ) ; $ this -> form_validation -> set_rules ( 'store' , 'Store' , 'trim|required' ) ; $ this -> form_validation -> set_rules ( 'availability' , 'Availability' , 'trim|required' ) ; if ( $ this -> form_validation -> run ( ) == TRUE ) { $ data = array ( 'name' => $ this -> input -> post ( 'product_name' ) , 'sku' => $ this -> input -> post ( 'sku' ) , 'cost_price' => $ this -> input -> post ( 'cost_price' ) , 'price' => $ this -> input -> post ( 'price' ) , 'qty' => $ this -> input -> post ( 'qty' ) , 'description' => $ this -> input -> post ( 'description' ) , 'attribute_value_id' => json_encode ( $ this -> input -> post ( 'attributes_value_id' ) ) , 'brand_id' => json_encode ( $ this -> input -> post ( 'brands' ) ) , 'category_id' => json_encode ( $ this -> input -> post ( 'category' ) ) , 'store_id' => $ this -> input -> post ( 'store' ) , 'availability' => $ this -> input -> post ( 'availability' ) , ) ; if ( $ _FILES [ 'product_image' ] [ 'size' ] > 0 ) { $ upload_image = $ this -> upload_image ( ) ; $ upload_image = array ( 'image' => $ upload_image ) ; $ this -> model_products -> update ( $ upload_image , $ product_id ) ; } $ update = $ this -> model_products -> update ( $ data , $ product_id ) ; if ( $ update == true ) { $ this -> session -> set_flashdata ( 'success' , 'Successfully updated' ) ; redirect ( 'products/' , 'refresh' ) ; } else { $ this -> session -> set_flashdata ( 'errors' , 'Error occurred!!' ) ; redirect ( 'products/update/' . $ product_id , 'refresh' ) ; } } else { $ attribute_data = $ this -> model_attributes -> getActiveAttributeData ( ) ; $ attributes_final_data = array ( ) ; foreach ( $ attribute_data as $ k => $ v ) { $ attributes_final_data [ $ k ] [ 'attribute_data' ] = $ v ; $ value = $ this -> model_attributes -> getAttributeValueData ( $ v [ 'id' ] ) ; $ attributes_final_data [ $ k ] [ 'attribute_value' ] = $ value ; } $ this -> data [ 'attributes' ] = $ attributes_final_data ; $ this -> data [ 'brands' ] = $ this -> model_brands -> getActiveBrands ( ) ; $ this -> data [ 'category' ] = $ this -> model_category -> getActiveCategroy ( ) ; $ this -> data [ 'stores' ] = $ this -> model_stores -> getActiveStore ( ) ; $ product_data = $ this -> model_products -> getProductData ( $ product_id ) ; $ this -> data [ 'product_data' ] = $ product_data ; $ this -> render_template ( 'products/edit' , $ this -> data ) ; } }
If the validation is not valid, then it redirects to the edit product page
 If the validation is successfully then it updates the data into the database
 and it stores the operation message into the session flashdata and display on the manage product page
If the validation is not valid then it redirects to the edit product page If the validation is successfully then it updates the data into the database and it stores the operation message into the session flashdata and display on the manage product page
// true case // attributes  // false case