How to prevent duplicate data when importing excel using PHPexcel in Codeigniter

806 Views Asked by At
public function import_excel(){
        if (!$_FILES["file"]["name"]) {
            echo "Please upload excel file !";
        } else {
            $path = $_FILES["file"]["tmp_name"];
            $object = PHPExcel_IOFactory::load($path);
            foreach ($object->getWorksheetIterator() as $worksheet) {
                $highestRow = $worksheet->getHighestRow();
                $highestColumn = $worksheet->getHighestColumn();
                for ($row = 2; $row <= $highestRow; $row++) {
                    $group = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
                    $merchant_id = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
                    $login_id = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
                    $play_id = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
                    $mem_name = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
                    $data[] = array(
                        'group' => $group,
                        'merchant_id' => $merchant_id,
                        'login_id' => $login_id,
                        'play_id' => $play_id,
                        'mem_name' => $mem_name,
                    );
                }
            }
            $this->db->insert_batch('excel_files', $data);
        }
    }

This code is working when upload excel, but I wonder when user upload excel 50 rows, the day after upload again 65 rows, the 65 rows no need to duplicate.

1

There are 1 best solutions below

1
On

you can do some check, in your looping code

public function import_excel(){
        if (!$_FILES["file"]["name"]) {
            echo "Please upload excel file !";
        } else {
            $path = $_FILES["file"]["tmp_name"];
            $object = PHPExcel_IOFactory::load($path);
            foreach ($object->getWorksheetIterator() as $worksheet) {
                $highestRow = $worksheet->getHighestRow();
                $highestColumn = $worksheet->getHighestColumn();
                for ($row = 2; $row <= $highestRow; $row++) {
                    $group = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
                    $merchant_id = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
                    $login_id = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
                    $play_id = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
                    $mem_name = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
                    $data[] = array(
                        'group' => $group,
                        'merchant_id' => $merchant_id,
                        'login_id' => $login_id,
                        'play_id' => $play_id,
                        'mem_name' => $mem_name,
                    );
                }
            }
            //-- check duplicate here
            $dataCheck = $this->methodCheck($data);
            if($dataCheck==true)
            {
                 $this->db->insert_batch('excel_files', $data);
            }
        }
    }   
    
    function methodCheck($param){
        $this->db->select("*");
        $this->db->from("yourInsertedTable");
        foreach($param as $searchKey=>$searchValue){
            $this->db->where($searchKey,$searchValue);
        }
        $hasil=$this->db->get('')->result_array();
        if(isset($hasil))
        {
            return false;
        }else{
            return true;
        }
    }