.csv file reading

I am trying to read an excel file that has been saved as a Comma Separated Values file.

When I open the file with a text editor each line consists of that which was on the respective row in the original Excel file. However, when I read the file using PHP each “line” has far more values than was in each row; PLUS I have no idea what PHP is using to decide where these lines start and end! This is probably causing some of my document to be cut out as well.

I am lost please help, perhaps I don’t understand what PHP means by a “line.” My code is below. I am starting at 10 so that the first row will be skipped, as there are 10 columns in the document.

<html>

<title>File Reader</title>

<body>

<?php

    $cell = 10;

    if (($handle = fopen("file.csv", "r")) !== FALSE)  {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $num = count($data);
                while ($cell < $num) {
                    for ($row=0; $row < 10; $row++) {
                        echo $data[$cell]." ";
                        $cell++;
                    }
                    echo "<br />";
                }
        }
        fclose($handle);
    }

?>

</body>

</html>

Please go through following:

PHP: Runtime Configuration - Manual

I think you’re reading it the wrong way. When you do a:

$data = fgetcsv($handle, 1000, ",")

the $data variable will contain all the fields for one row. Try simplifying your logic like this:

while (($data = fgetcsv($handle)) !== FALSE) {
	for ($i = 0; $i < count($data); $i++) {
		echo $data[$i] . " ";
	}
	echo "<br />\
";
}

Okay, thanks for the reply guys. The only line in my php.ini that relates to the Runtime Configuration is “allow_url_fopen = off” and this is not the default described in the manual on php.net.

So for some reason I am not getting a row with the fgetcsv() function using the following coded parameters. I get the value before the first comma.

It seems simple enough, but I can’t figure it out. What do you think is going wrong?

Thx again!!

<?php

$handle = fopen("file.csv", "r");
$data = fgetcsv($handle, 1000, ",");
echo $data[0];

?>

Thanks for your time. I see that my last post made very little sense.

It appears that there is an error in my file b/c the fgetcsv() loads the entire document (all values) into the array after only the first calling. A test document worked fine.