0

First, I've tried with simply one image uploading, then I've tried multiple images uploading with the same method but images uploaded to the server correctly but only one image name uploaded to the database. Then I've searched on google and try with for loop but it also shows undefined index error.

public function publish_post(){
        // publish post
            if ( $this->input->post('add_post') != '' ) {
                $this->form_validation->set_rules('p_name', 'Product Name','trim|required');
            $this->form_validation->set_rules('p_price', 'Product Price','trim|required');
            $this->form_validation->set_rules('p_currency_type', 'Currency Type');
            $this->form_validation->set_rules('p_category','Category','required');
            $this->form_validation->set_rules('p_menu','Menu','required');
            $this->form_validation->set_rules('p_country','Country','required');
            $this->form_validation->set_rules('p_state','State','required');
            $this->form_validation->set_rules('p_city','City','required');
            $this->form_validation->set_rules('p_zipcode','Zip Code','required');
            $this->form_validation->set_rules('p_description','Product Description','trim|required');

            // $config = ['upload_path' => './assets/postimg','allowed_types' => 'jpg|png|jpeg'];
                //$this->load->library('upload',$config);




            if ( $this->form_validation->run() /*&& $this->upload->do_upload('p_img1') && $this->upload->do_upload('p_img2') && $this->upload->do_upload('p_img3') && $this->upload->do_upload('p_img4') */) {
                // if validation OK then run this code


                $config = ['upload_path' => './assets/postimg','allowed_types' => 'jpg|png|jpeg'];
                $this->load->library('upload');
                $dataInfo = array();
                $files = $_FILES;
                $cpt = count($_FILES['userfile']['name']);
                for($i=0; $i<$cpt; $i++)
                {           
                    $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                    $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                    $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                    $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                    $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

                    $this->upload->initialize($config);
                    $this->upload->do_upload();

                    $dataInfo['userfile'] = $_FILES['userfile']['name'];

                    $files = array(

                        'p_img1' => $dataInfo[0],
                        'p_img2' => $dataInfo[1],
                        'p_img3' => $dataInfo[2],
                        'p_img4' => $dataInfo[3],
                        'owner_ins'=>$dataInfo[4]
                    );

                    $this->usermodel->add_post($files);

                }



                $p_name = $this->input->post('p_name');
                $p_price = $this->input->post('p_price');
                $p_currency_type = $this->input->post('p_currency_type');

                // $p_img1 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                // $p_img2 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                // $p_img3 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                // $p_img4 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                // $owner_ins = base_url('./assets/inspection/'.$this->upload->data('owner_ins').$this->upload->data('file_ext'));


                $p_category = $this->input->post('p_category');
                $p_menu = $this->input->post('p_menu');
                $p_country = $this->input->post('p_country');
                $p_state = $this->input->post('p_state');
                $p_city = $this->input->post('p_city');
                $p_zipcode = $this->input->post('p_zipcode');
                $p_description = $this->input->post('p_description');
                $seo = $this->input->post('seo');
                $tags = $this->input->post('tags');
                $user_id = $this->input->post('user_id');
                $posted_date = date('l jS \of F Y h:i A');
                $status = "Active";

                $rental_store = $this->input->post('rental_store');
                $parts_store = $this->input->post('parts_store');


                    $store_id = $this->input->post('store_id');


                    $data = array(

                        'p_name' => $p_name,
                        'p_price' => $p_price,
                        'p_currency_type' => $p_currency_type,
                        // 'p_img1' => $p_img1,
                        // 'p_img2' => $p_img2,
                        // 'p_img3' => $p_img3,
                        // 'p_img4' => $p_img4,

                        // 'p_img1' => $dataInfo[0],
                  //       'p_img2' => $dataInfo[1],
                  //       'p_img3' => $dataInfo[2],
                  //       'p_img4' => $dataInfo[3],

                        'p_category' => $p_category,
                        'p_menu' => $p_menu,
                        'p_country' => $p_country,
                        'p_state' => $p_state,
                        'p_city' => $p_city,
                        'p_zipcode' => $p_zipcode,
                        'p_description' => $p_description,
                        'user_id' => $user_id,
                        'store_id'=>$store_id,
                        'posted_date'=>$posted_date,
                        'status'=>$status,
                        'seo'=>$seo,
                        'tags'=>$tags,



                    );


                 $this->usermodel->add_post($data);

                 $fetch_store_posts_col = $this->storesmodel->fetch_store_posts_col($store_id);

                    foreach ($fetch_store_posts_col as $store_posts) {
                        //
                        $total_posts = $store_posts->posts;

                    }

                    $pluspost = $total_posts+1;

                    $newdata = array(

                        'posts'=>$pluspost

                    );

                 $this->storesmodel->store_post_count($store_id,$newdata);
                 $cate_posts = $this->usermodel->fetch_cate_for_update($p_category = $this->input->post('p_category'));
                 $update_cate_posts['posts'] = $cate_posts+1;
                 $this->usermodel->update_categories($p_category = $this->input->post('p_category'),$update_cate_posts);


                 redirect('user/add_post');




            }else{
                // if validation not OK then show error
                $cates = $this->adminmodel->categories();
                $country = $this->adminmodel->fetch_country();
                $state = $this->adminmodel->fetch_state();
                $city = $this->adminmodel->fetch_city();
            $zipcode = $this->adminmodel->fetch_zipcode();

                $userdata = $this->usermodel->userdata();


                foreach ($userdata as $user ) {
                    //
                    $owner_id = $user->user_id;
                }

                $store_count = $this->usermodel->store_count($owner_id);

                $upload_error = "error";//array('error' => $this->upload->display_errors());

                $this->load->view('user/add_post',['cates'=>$cates,'country'=>$country,'state'=>$state,'city'=>$city,'zipcode'=>$zipcode,'userdata'=>$userdata,'store_count'=>$store_count],$upload_error);
            }



            }

    }
Karlo Kokkak
  • 3,606
  • 4
  • 15
  • 31
  • 4
    Possible duplicate of [Multiple image upload with CodeIgniter](https://stackoverflow.com/questions/40778683/multiple-image-upload-with-codeigniter) – Pradeep May 02 '18 at 14:01
  • 1
    Possible duplicate of [Multiple files upload in Codeigniter](https://stackoverflow.com/questions/20113832/multiple-files-upload-in-codeigniter) – louk May 02 '18 at 14:08

1 Answers1

0

You're storing saved images wrongly. You were just overwriting previous values rather than adding new ones.

Fixed saving images to database by putting its code outside for loop when all images have been uploaded and ready for database insertion.

Adding a post records happens twice. Removed the first one which was unneeded.

Updated Code:

<?php
    public function publish_post(){
            // publish post
                if ( $this->input->post('add_post') != '' ) {
                    $this->form_validation->set_rules('p_name', 'Product Name','trim|required');
                $this->form_validation->set_rules('p_price', 'Product Price','trim|required');
                $this->form_validation->set_rules('p_currency_type', 'Currency Type');
                $this->form_validation->set_rules('p_category','Category','required');
                $this->form_validation->set_rules('p_menu','Menu','required');
                $this->form_validation->set_rules('p_country','Country','required');
                $this->form_validation->set_rules('p_state','State','required');
                $this->form_validation->set_rules('p_city','City','required');
                $this->form_validation->set_rules('p_zipcode','Zip Code','required');
                $this->form_validation->set_rules('p_description','Product Description','trim|required');
                // $config = ['upload_path' => './assets/postimg','allowed_types' => 'jpg|png|jpeg'];
                    //$this->load->library('upload',$config);

                if ( $this->form_validation->run() /*&& $this->upload->do_upload('p_img1') && $this->upload->do_upload('p_img2') && $this->upload->do_upload('p_img3') && $this->upload->do_upload('p_img4') */) {
                    // if validation OK then run this code
                    $config = ['upload_path' => './assets/postimg','allowed_types' => 'jpg|png|jpeg'];
                    $this->load->library('upload');
                    $dataInfo = array();
                    $files = $_FILES;
                    $cpt = count($_FILES['userfile']['name']);

            for($i=0; $i<$cpt; $i++){           
                $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

                $this->upload->initialize($config);
                $this->upload->do_upload();
                $dataInfo['userfile'][] = $_FILES['userfile']['name'];    
            }

            $files = array(
                        'p_img1' => isset($dataInfo['userfile'][0])?$dataInfo['userfile'][0]:'',
                        'p_img2' => isset($dataInfo['userfile'][1])?$dataInfo['userfile'][1]:'',
                        'p_img3' => isset($dataInfo['userfile'][2])?$dataInfo['userfile'][2]:'',
                        'p_img4' => isset($dataInfo['userfile'][3])?$dataInfo['userfile'][3]:'',
                        'owner_ins'=>isset($dataInfo['userfile'][4])?$dataInfo['userfile'][4]:''
                );

        //$this->usermodel->add_post($files);

                    $p_name = $this->input->post('p_name');
                    $p_price = $this->input->post('p_price');
                    $p_currency_type = $this->input->post('p_currency_type');

                    // $p_img1 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                    // $p_img2 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                    // $p_img3 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                    // $p_img4 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                    // $owner_ins = base_url('./assets/inspection/'.$this->upload->data('owner_ins').$this->upload->data('file_ext'));


                    $p_category = $this->input->post('p_category');
                    $p_menu = $this->input->post('p_menu');
                    $p_country = $this->input->post('p_country');
                    $p_state = $this->input->post('p_state');
                    $p_city = $this->input->post('p_city');
                    $p_zipcode = $this->input->post('p_zipcode');
                    $p_description = $this->input->post('p_description');
                    $seo = $this->input->post('seo');
                    $tags = $this->input->post('tags');
                    $user_id = $this->input->post('user_id');
                    $posted_date = date('l jS \of F Y h:i A');
                    $status = "Active";

                    $rental_store = $this->input->post('rental_store');
                    $parts_store = $this->input->post('parts_store');


                        $store_id = $this->input->post('store_id');


                        $data = array(

                            'p_name' => $p_name,
                            'p_price' => $p_price,
                            'p_currency_type' => $p_currency_type,
                            // 'p_img1' => $p_img1,
                            // 'p_img2' => $p_img2,
                            // 'p_img3' => $p_img3,
                            // 'p_img4' => $p_img4,

                             'p_img1' => isset($dataInfo['userfile'][0])?$dataInfo['userfile'][0]:'',
                             'p_img2' => isset($dataInfo['userfile'][1])?$dataInfo['userfile'][1]:'',
                             'p_img3' => isset($dataInfo['userfile'][2])?$dataInfo['userfile'][2]:'',
                             'p_img4' => isset($dataInfo['userfile'][3])?$dataInfo['userfile'][3]:'',

                            'p_category' => $p_category,
                            'p_menu' => $p_menu,
                            'p_country' => $p_country,
                            'p_state' => $p_state,
                            'p_city' => $p_city,
                            'p_zipcode' => $p_zipcode,
                            'p_description' => $p_description,
                            'user_id' => $user_id,
                            'store_id'=>$store_id,
                            'posted_date'=>$posted_date,
                            'status'=>$status,
                            'seo'=>$seo,
                            'tags'=>$tags,

                        );

                     $this->usermodel->add_post($data);
                     $fetch_store_posts_col = $this->storesmodel->fetch_store_posts_col($store_id);

                        foreach ($fetch_store_posts_col as $store_posts) {
                            //
                            $total_posts = $store_posts->posts;

                        }

                        $pluspost = $total_posts+1;

                        $newdata = array(

                            'posts'=>$pluspost

                        );

                     $this->storesmodel->store_post_count($store_id,$newdata);
                     $cate_posts = $this->usermodel->fetch_cate_for_update($p_category = $this->input->post('p_category'));
                     $update_cate_posts['posts'] = $cate_posts+1;
                     $this->usermodel->update_categories($p_category = $this->input->post('p_category'),$update_cate_posts);    
                     redirect('user/add_post');

                }else{
                    // if validation not OK then show error
                    $cates = $this->adminmodel->categories();
                    $country = $this->adminmodel->fetch_country();
                    $state = $this->adminmodel->fetch_state();
                    $city = $this->adminmodel->fetch_city();
                    $zipcode = $this->adminmodel->fetch_zipcode();

                    $userdata = $this->usermodel->userdata();

                    foreach ($userdata as $user ) {
                        $owner_id = $user->user_id;
                    }

                    $store_count = $this->usermodel->store_count($owner_id);
                    $upload_error = "error";//array('error' => $this->upload->display_errors());

                    $this->load->view('user/add_post',['cates'=>$cates,'country'=>$country,'state'=>$state,'city'=>$city,'zipcode'=>$zipcode,'userdata'=>$userdata,'store_count'=>$store_count],$upload_error);
                }

               }
        }

        ?>
Karlo Kokkak
  • 3,606
  • 4
  • 15
  • 31