-1

I have a function that inside my class that is handled 3 pieces of data.

  1. filename to be parsed
  2. replace string which is basically the insert or replace text except for the values.
  3. the tablename to hold the data or delete for getting new data.

My main concern is that I can get accurate count of records new/adjusted by this function

Here is the code for just this function.


    /**
 * @return int|string
 *
 * @psalm-return 0|positive-int|string
 */
private function csvimport(string $filename,string $replace, string $tablename) {

    // verify file exists
    if (!file_exists($filename)) {

        // return error message
        return 'File not found at '.$filename;
        
    }
    
    // set counter
    $counter = 0;
    $headcount = 0;

    // now read file line by line skipping line 1
    $file = fopen($filename, 'r');

    while (($line = fgetcsv($file)) !== FALSE) {

        // check if counter is greater than zero
        if ($counter > 0) {
            
            // get number of columns in this line
            $colcount = count($line);

            // replace inside array?
            $line = str_replace('"','',$line);
            
            // convert array to comma-delimmited
            $values = '"'.implode('","', $line).'"';
            
            // compare colcount to headcount
            while ($colcount < $headcount) {

                // add comma to the end of values
                $values .= ',';
                
                // get new colcount
                $colcount = count($values);
                
            }
            
            

            // create sql string
            $sql = $replace.$values.',"","","","")';

            // send this to do an update
            $res = array();
            $res = $this->sql_update($sql);
            
            // get num rows and status
            $status = $res[0];
            $num_rows = $res[1];
            
            // assemble status string
            $status = $status . ' [# Of Rows '.$num_rows.']';

            // do sql tracker
            $this->sqltracker($sql);
            $this->sqltracker($status);

            
        } else {
            
            // count number of columns in header
            $headcount = count($line);
        }
        
        // increment counter
        $counter++;
        
        // reset colcount
        $colcount = 0;
    }

    // close file
    fclose($file);
    
    // return
    return $counter;
    
    
}
crosenblum
  • 1,869
  • 5
  • 34
  • 57

1 Answers1

0

The problem is you deal array as string and string as array. Here you deal array as string:

$line = str_replace('"','',$line);
    

I think this should be:

foreach($line as &$clm){
    $clm=str_replace('"','',$clm);
}
    

Here you deal string as array:

$colcount = count($values);
    

change it this to:

$colcount++;

Since you didn't show the implementation of sql_update function, We can't expect why you write code like this: $sql = $replace.$values.',"","","","")';

M3Ali
  • 141
  • 1
  • 4
  • The main problem is the $counter++; which is what I do to get the number of rows that are parsed in the csv, but it is always off. Every day I generate new csv's, but the code shows no data being updated/inserted. – crosenblum Oct 05 '21 at 01:57