skip to Main Content

I am using codeigniter 3 and when I uploaded data to my database, it didn’t skip the duplicates.

May someone please help me to address this problem?

The code below is used to upload excel files. I’d like to skip duplicate records when a user uploads a previously uploaded excel again.

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'];
            }
        }
    }

2

Answers


  1. 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..!

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search