0
votes

I've been using this script in the past and was ok. I converted txt file to csv and have got some undefinded error.

function Importcsv($filename) { $row = 0;
$col = 0;

$handle = @fopen($filename, "r");
if ($handle) 
{
    while (($row = fgetcsv($handle, 4096)) !== false) 
    {
        if (empty($fields)) 
        {
            $fields = $row;
            continue;
        }

        foreach ($row as $k=>$value) 
        {
            $results[$col][$fields[$k]] = $value;
        }   
        $col++;
        unset($row);
    }
    if (!feof($handle)) 
    {
        echo "Error: unexpected fgets() failn";
    }
    fclose($handle);
}

return $results; }


$filename = "tm_data.csv";
$csvArray = Importcsv($filename);

foreach ($csvArray as $row)
{
 echo $row['CITY'];
}

Please let me know if you have any idea or discovered similar issues before.

Please see few error lines Notice: Undefined offset: 386 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20

Notice: Undefined offset: 387 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20

Notice: Undefined offset: 388 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20

Notice: Undefined offset: 389 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20

Notice: Undefined offset: 390 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20

Thanks,

1
Post the full error message, without it we can't help you.AntoineB
Please see below few lines of error messages:Notice: Undefined offset: 386 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20 Notice: Undefined offset: 387 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20 Notice: Undefined offset: 388 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20 Notice: Undefined offset: 389 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20 Notice: Undefined offset: 390 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20user4773611
@user4773611 include the errors in your question. You can do this by editting your question.Epodax

1 Answers

1
votes

From the (lack of) information posted in the question I think line 20 is:

 $results[$col][$fields[$k]] = $value;

It seems that the first row of your CSV file contains less columns than the data rows.

The first row (the column names) is stored in $fields and it has 386 columns. Some of the data rows contain more than 386 columns (391, it seems) and for $k starting with 386, $fields[$k] is undefined and triggers the notices you posted.

The solution is up to you. You can fix the CSV file by adding the missing column names or by removing the extra values from the offending row(s).

You can also modify the code to do one of the above (either generate some column names on-the-fly or ignore the extra values).

You should also check if the CSV file is properly encoded. Maybe there is no extra column and fgetcsv() incorrectly split the row. Check the separator, quote and escape characters used in the file and make sure you use the same values in the call to fgetcsv().