【问题标题】:How to prevent duplicate data when importing excel using PHPexcel in CodeigniterCodeigniter中使用PHPexcel导入excel时如何防止重复数据
【发布时间】:2023-03-25 21:45:01
【问题描述】:
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);
        }
    }

此代码在上传excel时有效,但我想知道当用户上传excel 50行时,第二天再次上传65行,这65行不需要重复。

【问题讨论】:

    标签: php codeigniter phpexcel-1.8.0


    【解决方案1】:

    你可以在你的循环代码中做一些检查

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

    【讨论】:

    • 您好@Hasan先生,我已经尝试过您的代码,但没有在数据库中插入数据。
    猜你喜欢
    • 2015-09-11
    • 1970-01-01
    • 2018-01-21
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 2013-05-01
    相关资源
    最近更新 更多