PHP Classes

fgetcsv

Recommend this page to a friend!

      CSV Handler  >  All threads  >  fgetcsv  >  (Un) Subscribe thread alerts  
Subject:fgetcsv
Summary:fgetcsv seems to truncate empty columns on the end
Messages:2
Author:Richard Wilson
Date:2005-10-03 02:30:43
Update:2005-10-03 10:14:14
 

  1. fgetcsv   Reply   Report abuse  
Picture of Richard Wilson Richard Wilson - 2005-10-03 02:30:43
I exported my phone list from Outlook in CSV format.
With CSVHandler, I got undefined index errors on line 37. I investigated and found that fgetcsv did not get the right number of columns. To fix this, I changed the following code:
while ($DataLine = fgetcsv ($fp, 3000, $this->Separator)) {
for($i=0;$i<count($this->HeaderData);$i++){
$Item[$this->HeaderData[$i]]=$DataLine[$i];
to:
while ($DataLine = fgetcsv ($fp, 0, $this->Separator)) {
for($i=0;$i<count($this->HeaderData);$i++){
if ($i < count($DataLine))
{
$Item[$this->HeaderData[$i]]=$DataLine[$i];
} else
$Item[$this->HeaderData[$i]]='';

It seems to work now.

  2. Re: fgetcsv   Reply   Report abuse  
Picture of Andreas Mueller Andreas Mueller - 2005-10-03 10:14:14 - In reply to message 1 from Richard Wilson
Hi Richard.

Thanks for that information.
Your change does of course work as it provides correct definition for empty cells.

I'll use it for the next version. Just never occured to me that any program would write such truncated csv-files. Standard would require to generate an empty field to be exported as just a delimiter, and depending on settings a set of "" in case of a text field (wich is another csv-feature I have not taken care of, those are just removed :-)

Andreas