Linked Questions

Popular Questions

I have upload data into database via excel sheet upload. Now I want to generate a csv / excel file which contain the reports of successfully inserted data and data which cannot be uploaded with error message. I have no idea how to do this in codeigniter. And also it should be download automatically after csv file generation Below are my code, My controller:

 public function uploadData(){

     if ($this->input->post('pass')) {
               $path = APPPATH .'uploads/';
               require_once APPPATH . "/third_party/PHPExcel.php";
               $config['upload_path'] = $path;
               // echo  $config['upload_path'];exit;
               $config['allowed_types'] = 'xlsx|xls|csv';
               $config['remove_spaces'] = TRUE;
               $this->load->library('upload', $config);
               $this->upload->initialize($config);            
               if (!$this->upload->do_upload('uploadFile')) {
                   $error = array('error' => $this->upload->display_errors());
               } else {
                   $data = array('upload_data' => $this->upload->data());
               }
               if(empty($error)){
                 if (!empty($data['upload_data']['file_name'])) {
                   $import_xls_file = $data['upload_data']['file_name'];
               } else {
                   $import_xls_file = 0;
               }
               $inputFileName = $path . $import_xls_file;
              // echo $inputFileName;exit;
               try {
                   $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
                   $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                   $objPHPExcel = $objReader->load($inputFileName);
                   $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
                   $flag = true;
                   $i=0;
                   $afftectedinsertRows = 0;
                   $afftectedupdateRows = 0;
                   foreach ($allDataInSheet as $value) {
                     if($flag){
                       $flag =false;
                       continue;
                     }
                     $inserdata[$i]['emp_id'] = $value['A'];
                     $inserdata[$i]['nominee_fname'] = $value['B'];
                     $inserdata[$i]['nominee_lname'] = $value['C'];
                     $inserdata[$i]['nominee_dob'] = date("d-m-Y", strtotime($value['D']));
                     $inserdata[$i]['share_percentile'] = $value['E'];
                     $inserdata[$i]['policy_id'] = $this->input->post('policy_name');

                     $query3 = $this->db->select('*')
                                        ->from('member_policy_nominee')
                                        ->where('emp_id', $inserdata[$i]['emp_id'])
                                        ->get()
                                        ->result_array();
                                       $share = array_column($query3,"share_percentile");
                                       $nominee = array_column($query3,"nominee_id");
                                       $ary = array_sum($share);
                                       $sum = $ary + $inserdata[$i]['share_percentile'];

                    $query = $this->db->select('*')
                                      ->from('member_policy_nominee')
                                      ->where('emp_id', $inserdata[$i]['emp_id'])
                                      ->where('nominee_fname', $inserdata[$i]['nominee_fname'])
                                      ->where('nominee_lname', $inserdata[$i]['nominee_lname'])
                                      ->get();

        if($query -> num_rows() == 0 ) {

                if($sum >= "100" || $inserdata[$i]['share_percentile'] > "100"){

                   $update[$i]['status_upload'] = "Share Percntile Should not exceed 100";
            $this->db->where('emp_id', $inserdata[$i]['emp_id'])
            ->update('member_policy_nominee', $update[$i]);
                    // return ['status' => "Share Percntile Should not exceed 100"];
                  // echo $this->db->last_query();exit;
                  }else{
                   $inserdata[$i]['status_upload'] = "Data inserted successfully";
                    $result =  $this->db->insert('member_policy_nominee', $inserdata[$i]);
                    $afftectedinsertRows = $this->db->affected_rows();
                     // return ['status' => "Imported successfully"];
                       // echo $this->db->last_query();exit;
                 }

               }else{
            $update[$i]['status_upload'] = "Data already Exist";
            $this->db->where('emp_id', $inserdata[$i]['emp_id'])
            ->update('member_policy_nominee', $update[$i]);


}

   // echo $this->db->last_query();exit;
                     $i++;
                      $afftectedinsertRows++;
                      $afftectedupdateRows ++;
                   }  

                   // $result = $this->add_nominee->importdata($inserdata);   
                   if(isset($result)){
                     echo "Imported successfully";
                     // echo   $afftectedinsertRows++;
                   }else{
                     echo "Share Percntile Should not exceed 100";
                     // echo   $afftectedupdateRows ++;;
                   }             

             } catch (Exception $e) {
                  die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                           . '": ' .$e->getMessage());
               }
             }else{
                 echo $error['error'];
               }

       }

     }

Related Questions