Trying to parse .csv

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jlimbo
Forum Newbie
Posts: 10
Joined: Thu Nov 19, 2009 9:33 pm

Trying to parse .csv

Post 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>";
?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Trying to parse .csv

Post 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.
jlimbo
Forum Newbie
Posts: 10
Joined: Thu Nov 19, 2009 9:33 pm

Re: Trying to parse .csv

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Trying to parse .csv

Post by requinix »

Hint: fgetcsv returns false when it runs out of stuff to read.
jlimbo
Forum Newbie
Posts: 10
Joined: Thu Nov 19, 2009 9:33 pm

Re: Trying to parse .csv

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Trying to parse .csv

Post 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.
jlimbo
Forum Newbie
Posts: 10
Joined: Thu Nov 19, 2009 9:33 pm

Re: Trying to parse .csv

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Trying to parse .csv

Post 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?
jlimbo
Forum Newbie
Posts: 10
Joined: Thu Nov 19, 2009 9:33 pm

Re: Trying to parse .csv

Post by jlimbo »

The if (is_array)... check should check every row individually as it is declared in the while loop.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Trying to parse .csv

Post 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?
jlimbo
Forum Newbie
Posts: 10
Joined: Thu Nov 19, 2009 9:33 pm

Re: Trying to parse .csv

Post by jlimbo »

I just replaced the source csv file and now it all works ok. Thanks all for your help

John
Post Reply