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,
                    );
                }
            }
            $this->db->insert_batch('excel_files', $data);
        }
    }

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

4

1 回答 1

0

您可以在循环代码中进行一些检查

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;
        }
    }
于 2021-10-15T08:14:08.167 回答