导入CSV跳过标题或第一行时 [英] When import CSV skip header or first row

查看:161
本文介绍了导入CSV跳过标题或第一行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是重复的问题.但是我已经尝试了在 https://stackoverflow.com/上找到的所有答案.

I know this is duplicate question. But I have tried all the answers which I have found on the https://stackoverflow.com/ .

我认为,如果与记录相同的日期匹配,则我将插入和更新数据;如果不匹配,则将更新数据.

I think in my case I am inserting and updating data if same date match than record will update if not than it will insert.

以下是我的文件代码:

<?php 
if ( isset( $_POST['submit'] ) && $_POST['upload-csv'] == 'upload' ) {
    $error = array();
    $success = array();
    $filename = $_FILES['file']['name'];
    $filetype = wp_check_filetype( $filename );

    if ( $filetype['ext'] == 'csv' && $filetype['type'] == 'text/csv' ) {

        $handle = fopen( $_FILES['file']['tmp_name'], "r" );
        $row = 0;
        $skip_row_number = array("1");
        while ( ($data = fgetcsv( $handle, 1000, "," )) !== FALSE ) {
            $data = array_map("utf8_encode", $data);

            if ($row > 0)   
            {

                $table_name = $wpdb->prefix . 'prayer';
                $ipquery = $wpdb->get_results("SELECT * FROM `$table_name` WHERE `date` = '".$data[0]."'");     
                $query_res = $wpdb->num_rows;   
                // Check if same date data 
                if($query_res >=1){
                    $updateQuery = "UPDATE `$table_name` SET 
                                            `date` = '".$data[0]."', 
                                            `first_start` = '".$data[1]."',
                                            `first_end` = '".$data[2]."', 
                                            `second_start` = '".$data[3]."', 
                                            `second_end` = '".$data[4]."', 
                                            `third_start` = '".$data[5]."', 
                                            `third_end` = '".$data[6]."', 
                                            `forth_start` = '".$data[7]."', 
                                            `forth_end` = '".$data[8]."', 
                                            `five_start` = '".$data[9]."', 
                                            `five_end` = '".$data[10]."', 
                                            `done` = '".$data[10]."'
                                            WHERE `$table_name`.`date` = '".$data[0]."';";
                    $up_res = $wpdb->query($updateQuery);
                }else{
                    $query = "INSERT INTO $table_name (date, first_start, first_end, second_start,
                             second_end, third_start, third_end, forth_start, forth_end, five_start, five_end, done) 
                             VALUES ('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."','".$data[4]."','".$data[5]."','".$data[6]."','".$data[7]."','".$data[8]."','".$data[9]."','".$data[10]."','".$data[11]."')";
                    $insert_res = $wpdb->query($query);
                }

            }

        $row++; 
        }
        fclose( $handle );
        $success[] = 'Import done.';
    } else {
        $error[] = 'Please upload CSV file only';
    }
}   
?>

我已经尝试过以下答案,以跳过标题:

I have tried the below answer for skip the header:

跳过第一行CSV文件

导入CSV,排除第一行

跳过fgetcsv的第一行php中的方法

帮我解决这个问题.

推荐答案

ParseCSV是从CSV获取数据的最新,最简单的方法,您可以轻松地从CSV获取数据的控制.

ParseCSV is latest and easy way to get data from CSV and you can get control of data from CSV easily.

您必须按照正确的路径添加库文件

in which you have to add library file as per proper path

e.g. require_once 'parsecsv.lib.php';

然后,它分别返回标题和数据.

After then it return title and data seperately.

$filename = 'abc.csv';  // path of CSV file or after upload pass temp path of file
$csv = new parseCSV();
$csv->auto($filename);

foreach ($csv->titles as $value):
$getcsvtitle[] = // get header in variable
endforeach;

foreach ($csv->data as $key => $row):
 //$getdataasperrow = // get data row wise.
endforeach;

仅添加库后,ParseCSV就以数组格式返回数据,因此您可以轻松地分隔标题和其他数据,并且与换行符和特殊字符兼容,而且我一直都在使用它

ParseCSV return data in array format after just adding library, so you can easily separate header and other data, and compatible to new line and special character as well as i have been always using it as well.

也请参考下面的链接以获取更多详细信息.

Please refer below link for further detail as well.

ParseCSV GitHub

这篇关于导入CSV跳过标题或第一行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆