Home > Blockchain >  how to skip excel sheet duplicate records when importing in database in codeigniter
how to skip excel sheet duplicate records when importing in database in codeigniter

Time:11-18

Hello everyone i am using codeigniter 3 and when i upload data in my datbase it didn't skip the duplicate
can you please help me to skip the dublicate data

This code is working when upload excel, but I wonder when user upload excel same sheet with dublicate data, after upload again whant it to skip dublicate data

Below is my controller

public function uploadData()
    {
        if ($this->input->post('submit')) {
            $path = 'uploads/';
            require_once APPPATH . "/third_party/PHPExcel.php";
            $config['upload_path'] = $path;
            $config['allowed_types'] = 'xlsx|xls';
            $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;

                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;
                    foreach ($allDataInSheet as $value) {
                        if ($flag) {
                            $flag = false;
                            continue;
                        }
                        $inserdata[$i]['SR_NO'] = $value['A'];
                        $inserdata[$i]['NTN'] = $value['B'];
                        $inserdata[$i]['NAME'] = $value['C'];
                        $inserdata[$i]['BUSINESS_NAME'] = $value['D'];

                        $i  ;
                    }
                    $result = $this->import_model->importdata($inserdata);
                    if ($result) {
                        echo "Imported successfully";
                    } else {
                        echo "ERROR !";
                    }
                } catch (Exception $e) {
                    die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                        . '": ' . $e->getMessage());
                }
            } else {
                echo $error['error'];
            }
        }
    }

CodePudding user response:

I think you can use array_unique() function to the variable which is storing all the data records in array format. This function returns all the unique elements and this way your duplicate data records can be removed..!

CodePudding user response:

You need to check every record form database first on the basis on primary or any combination of unique record, if it exist in db then update or skip that record else insert a new record.

  • Related