Page 1 of 1
Trying to parse .csv
Posted: Thu Nov 19, 2009 9:37 pm
by jlimbo
I'm trying to parse a csv file via php. The code below worked except for a warning about an invalid argument in the foreach() statement. I tried a couple of things to rectify the problem and none worked. I have completely removed all the changes (double and triple checked) and now I only get the warning and no data. Any ideas what the problem may be? It seems that $row is not accepted as an array, but the fgetcsv() function should return an array to it. I've tried to implicitly assign $row as an array and then push the csv into it, but then there is no data or warning.
Code: Select all
<?php
$fh = fopen("flightPoints.csv", "r");
$i=0;
echo "<table border = '1'>";
while(!feof($fh)) {
echo "<tr>";
$row = fgetcsv($fh,1000);
foreach($row as $key => $value) {
echo "<td>" . $value. "</td>";
}
echo "</tr>";
$i++;
}
echo "</table>";
?>
Re: Trying to parse .csv
Posted: Thu Nov 19, 2009 9:55 pm
by Jonah Bron
You should pay attention to error messages. Tell us what is reads.
Try printing the value of $row right after you define it, too.
Re: Trying to parse .csv
Posted: Thu Nov 19, 2009 10:10 pm
by jlimbo
The error message is
Warning: Invalid argument supplied for foreach() in C:\wamp\www\flightPoints\flightPoints.php on line 9
Tried to print $row, but no output
Re: Trying to parse .csv
Posted: Fri Nov 20, 2009 1:56 am
by requinix
Hint: fgetcsv returns false when it runs out of stuff to read.
Re: Trying to parse .csv
Posted: Fri Nov 20, 2009 8:25 am
by jlimbo
That should be taken care of by the while loop. The fgetcsv should only therefore execute when there is something for it to read
Re: Trying to parse .csv
Posted: Fri Nov 20, 2009 8:37 am
by Eran
Not every row necessarily contains CSV data (there could be empty rows to separate batches of data). You should put a check regardless on whether fgetcsv returns false or not.
Re: Trying to parse .csv
Posted: Fri Nov 20, 2009 9:24 am
by jlimbo
Ok, I've put a check in code as follows, which gets rid of the warning but displays "Error". $row, therefore is not an array, I get that. But it should keep running the fgetcsv til the eof is reached. Therefore, unless the file is empty (which it isn't) then the remining rows in the file should be displayed. Or am I completly missing something?
Code: Select all
<?php
$fh = fopen("flightPoints.csv", "r");
$i=0;
echo "<table border = '1'>";
while(!feof($fh)) {
echo "<tr>";
$row = fgetcsv($fh,1000);
if (is_array($row)) {
foreach($row as $key => $value) {
echo "<td>" . $value. "</td>";
}
}
else {
echo "<tr>Error</tr>";
}
echo "</tr>";
$i++;
}
echo "</table>";
?>
Thanks for your help guy's, much appreciated
Re: Trying to parse .csv
Posted: Fri Nov 20, 2009 9:40 am
by Eran
Did you check that any of the rows are actually array? is it possible the delimiter is different from the default (a comma) - maybe a space or a semi-colon?
Re: Trying to parse .csv
Posted: Fri Nov 20, 2009 10:01 am
by jlimbo
The if (is_array)... check should check every row individually as it is declared in the while loop.
Re: Trying to parse .csv
Posted: Fri Nov 20, 2009 11:33 am
by Eran
yes.. but you are saying you are getting no data, which means that no row is actually an array. That's what I meant. Did you check the delimiter?
Re: Trying to parse .csv
Posted: Sat Nov 21, 2009 6:59 am
by jlimbo
I just replaced the source csv file and now it all works ok. Thanks all for your help
John