troubleshooting Question

Read CSV file in PHP

Avatar of motolanix1
motolanix1 asked on
PHP
1 Comment1 Solution285 ViewsLast Modified:
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";
}
?>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros