Welcome Guest, Not a member yet? Register   Sign In
Please help How to check duplicate data when import data excel in codeigniter 3
#1

I want import data excel into mysql using codeigniter 3 but when there are some data duplicate will show a message and skip the duplicate data. this is my controller :

Code:
public function import_excel(){
    if(isset($_FILES["fileExcel"]["name"])){
        $path=$_FILES["fileExcel"]["tmp_name"];
        $object=PHPExcel_IOFactory::load($path);
        foreach($object->getWorksheetIterator() as $worksheet)
        {       
            $highestRow=$worksheet->getHighestRow();
            $highestColumn=$worksheet->getHighestColumn();
            for($row=2; $row<=$highestRow; $row++)
            {
                $password=$worksheet->getCellByColumnAndRow(4,$row)->getValue();
                $name=$worksheet->getCellByColumnAndRow(1, $row)->getValue();
                $company=$worksheet->getCellByColumnAndRow(3,$row)->getValue();
                $email=$worksheet->getCellByColumnAndRow(2, $row)->getValue();
                $mobile=$worksheet->getCellByColumnAndRow(5,$row)->getValue();
                $level=$worksheet->getCellByColumnAndRow(6,$row)->getValue();
                $active=$worksheet->getCellByColumnAndRow(7,$row)->getValue();
             
                $temp_data[]=array(
                    'name'=>$name,
                    'company'=>$company,
                    'email'=>strtolower($email),
                    'password'=>password_hash(($password),PASSWORD_DEFAULT),
                    'mobile'=>$mobile,
                    'level'=>$level,
                    'active'=>$active
                    );
               
                 
                         
            }
           
                $this->m_data->insert($temp_data);                     
                redirect('userlist');
            }   
       
                   
                             
        }
     
    }
this is my model :
Code:
  public function insert($temp_data){
  $this->db->insert_batch('user',$temp_data);

}
Thank you very much I hope I find for this solution.
Reply
#2

@dhoengpreth ,

Or... I would make this a two step process. First, import data into a temporary database table and check it to eliminate duplicate records. You can also notify the user of this issue. Once the duplicate issues are resolved then update the main database table with the new records.
Reply
#3

You can add a validation before adding the new data to the array.

So, before doing this:

Code:
$temp_data[]=array(
    'name'=>$name,
    'company'=>$company,
    'email'=>strtolower($email),
    'password'=>password_hash(($password),PASSWORD_DEFAULT),
    'mobile'=>$mobile,
    'level'=>$level,
    'active'=>$active
);

You have to check if there is no data already exists in the array (if you can give the condition which is the duplicate mean, maybe we can give the example).

Code:
if (NOT EXISTS) { // Add to array
  $temp_data[] = ....
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB