Read CSV file in PHP

Hi Experts,

I have this code below to read the contents of a CSV file and display on a webpage. It should also alternate the background color of the row i.e. white, blue, white blue etc. But i keep getting the following error:

Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in C:\XAMPP\xampp\htdocs\products.php on line 51

Can you please check the code and test it and let me know why i am getting this error? Thanks. Speedy response is as usual very much appreciated.

<?php
                        
$dir_name = "C:/htdocs/catalogue.txt";
$fh = fopen($dir_name, "r");

$c = 0;

while(($data = fgetcsv($fh, 1000, ",")) !== FALSE)
{
$c++;
echo "<tr bgcolor=\""
if ($c % 2 == 1) echo "#0000FF"; else echo "#FFFFFF";
echo "\">\n";
$num = count($data); // count the # of lines that exist
$row++;  //increment the row variable to the next line
   
//output current line to the screen.
for ($i=0; $i < $num; $i++)
{
echo "<td>\n";
echo $data[$i]."\n";
echo "</td>\n";
}
echo "</tr>\n";
}
?>
motolanix1Asked:
Who is Participating?
 
jazzIIIloveConnect With a Mentor Commented:
Catch it.

Best regards.
<?php
                        
$dir_name = "C:/catalogue.csv"; 
$fh = fopen($dir_name, "r"); 

$c = 0;

while(($data = fgetcsv($fh, 1000, ",")) !== FALSE)
{
$c++;
echo "<tr bgcolor=\"";
if ($c % 2 == 1) echo "#0000FF"; else echo "#FFFFFF";
echo "\">\n";
$num = count($data); // count the # of lines that exist
$row++;  //increment the row variable to the next line
   
//output current line to the screen.
for ($i=0; $i < $num; $i++)
{
echo "<td>\n";
echo $data[$i]."\n";
echo "</td>\n";
}
echo "</tr>\n";
} 
?>

Open in new window

0
All Courses

From novice to tech pro — start learning today.