PHP批量上传csv文件

时间:2014-07-09 06:28:52

标签: php sql codeigniter

$data['error'] = '';    //initialize image upload error array to empty

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'csv';
    $config['max_size'] = '10000';

    $this->load->library('upload', $config);


    // If upload failed, display error
    if (!$this->upload->do_upload()) {
        $data['error'] = $this->upload->display_errors();
        $data['mainpage']='masterorder';
        $data['mode']='masterorders';
        $this->load->view('includes/mainpage', $data);
    } else {
        $file_data = $this->upload->data();
        $file_path =  './uploads/'.$file_data['file_name'];

        if ($this->csvimport->get_array($file_path)) {
            $csv_array = $this->csvimport->get_array($file_path);
            $this->db->trans_begin();
            $successflag=true;
            foreach ($csv_array as $row) {
                //                  


                $order = array(
                    'sheet_type'=>$this->input->post('sheet_type'),
                    'order_id'=>$row['OrderId'], 
                    'order_name'=>$row['OrderName'],
                    'department'=>$row['Department'],
                    'gender'=>$row['Gender'],
                    'maturity'=>$row['Maturity'],
                    'group_name'=>$row['GroupName'],
                'cat_id'=>$row['Category'],
                'sub_cat_id'=>$row['Sub-category'],

                );
                //$this->db->trans_begin();
                $query = $this->db->query("select count(*) cnt from order_master where order_id='{$order['order_id']}'");
                $row = $query->first_row();
                if(trim($order['order_id'])!="" && $row->cnt==0 )
                //if ($this->db->trans_status() === FALSE)
                {

                    $this->masterorder_model->order($order);                     
                }
                else if (  $row->cnt>0)
                {
                   $successflag=false;
                   break;
                }


            }


            if(!$successflag)
            {
                $this->db->trans_rollback();
                $this->flash->success('<h4>Duplicate Values Found.</h4>');
            }
            else
            {
                 $this->db->trans_commit();
                $this->flash->success('<h4>Csv Data Imported Successfully.</h4>');
            }
            redirect(base_url().'masterorder/index');

        }
        else
        $data['error'] = "Error occured";
        $data['mainpage']='masterorder';
        $data['mode']='masterorders';
        $this->load->view('includes/mainpage', $data);
    }
}

这是我的代码,同时批量上传一个csv文件iam得到验证重复值如果有相同的重复ID,我怎么能得到那个重复的id我应该显示id也可以任何人给我的想法。< / p>

1 个答案:

答案 0 :(得分:1)

如果要上传大量数据,您的方法会非常慢。你可以更好地利用MySQL的功能。这里有一些伪代码可以帮助您入门:

Upload CSV file to server

Create temporary table in MySQL database for data to be imported

Load data to temporary table with LOAD DATA INFILE

Find duplicates with an inner join
   SELECT `id` from `current_table` c inner join `temp_table` t on c.id = t.id;

If number_of_rows_returned = 0 {
   merge data with Insert...SELECT
} else {
   create report from data selected above
}
DROP temporary table