Welcome Guest, Not a member yet? Register   Sign In
Validation and Upload simultaneously
#1

[eluser]haily_php[/eluser]
I have product's form which consists of many various fields as: product's name, price, image,... I have checked one-by-one condition.

* Upload for image:
Code:
if(isset($_FILES['product_img']) && !empty($_FILES['product_img']['name'])){
                        if(!$this->upload->do_upload("product_img")){
                            $data['error'] =$this->upload->display_errors();
                            $this->my_layout->view("add_v",$data);
                            
                        }else{
                            $data=$this->upload->data();
                                $config['image_library'] = 'gd2';
                                $config['source_image']    = './uploads/product/'.$data['file_name'];
                                $config['create_thumb'] = TRUE;
                                $config['maintain_ratio'] = TRUE;
                                $config['width']     = 200;
                                $config['height']    = 95;
                            
                            $this->load->library('image_lib', $config);
                            $this->image_lib->resize();    
                            $image= $data['file_name'];////     When anyone does not click to upload, program will save image's field to be $data['file_name']
                            }
            }else{
                    $image=""; //     When anyone does not click to upload, program will save image's field to be ""
            }


* Vaildation for other field:

Code:
if($this->form_validation->run()==FALSE){
            $this->my_layout->view("add_v",$data);    
            }

I went them in "if... else loop" and all of them that are ok. But, when I want to save them, I have problem in Image's field, program is not understand. So what will I want to do to save all of them successfully. I have tried many other ways but it has not run exact.

All my codes:

Code:
if($this->form_validation->run()==FALSE){
            $this->my_layout->view("add_v",$data);    
            }else if(isset($_FILES['product_img']) && !empty($_FILES['product_img']['name'])){
                        if(!$this->upload->do_upload("product_img")){
                            $data['error'] =$this->upload->display_errors();
                            $this->my_layout->view("add_v",$data);
                            
                        }else{
                            $data=$this->upload->data();
                                $config['image_library'] = 'gd2';
                                $config['source_image']    = './uploads/product/'.$data['file_name'];
                                $config['create_thumb'] = TRUE;
                                $config['maintain_ratio'] = TRUE;
                                $config['width']     = 200;
                                $config['height']    = 95;
                            
                            $this->load->library('image_lib', $config);
                            $this->image_lib->resize();    
                            $image= $data['file_name'];
                            }
            }else if(!isset($_FILES['product_img'])){
                        $image= "";
            }else if(?????){ // I don't know any right condition
                $data1=array(
                    "name"     =>    $this->input->post("product_name"),
                    "image"    =>    $image, // true or false
                    "content"        =>    $this->input->post("product_content"),
                    "price"            =>    $this->input->post("product_old_price"),,
                    "visible"        =>    $this->input->post("visible"),
                    );
                    
        
            
            echo "<pre>";
            print_r($data1);
            echo"</pre>";
        
    }
#2

[eluser]skunkbad[/eluser]
What I do is call $this->upload->do_upload() from within my if statement when validation does not fail. Assuming you are tracking which photo belongs to the product, you would insert your photo details in your database and pass back the last insert ID. If there is no last insert ID, or if upload fails, you simply output an error message. Simple stuff.
#3

[eluser]haily_php[/eluser]
with your opinion, I will insert detail product first, then, I will check and insert image.




Theme © iAndrew 2016 - Forum software by © MyBB