1

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.

Below is my file code:

<?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:

Skip the first line of a CSV file

Import CSV, exclude first row

skip first line of fgetcsv method in php

Help me sort out this issue.

Community
  • 1
  • 1
mageDev0688
  • 566
  • 4
  • 27

1 Answers1

1

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

Ashish Patel
  • 3,551
  • 1
  • 15
  • 31