First off, I would like to say that I am just starting with PHP so please be kind. What I am trying to do is print a table from a csv file. The first row I want to be bold with a gray background color. The next rows should alternate with white and green backgrounds with text that is not bold. Below is my attempt, but it just produces a table with all bold text that has a gray background
<?php
echo "<html><body><table>\n\n";
$f = fopen("acsv.csv", "r");
$green = "#DDFFCC";
$white = "#FFFFFF";
$grey = "#EEEEEE";
$color = "#EEEEEE";
echo "<table border=1 cellpadding=4 cellspacing=0>";
echo "<tr>";
while (($line = fgetcsv($f)) !== false) {
foreach ($line as $cell) {
if ($color = "#EEEEEE"){
echo "<td bgcolor= ".$color."><b>" . htmlspecialchars($cell) . "</b></td>";
}
else{
echo "<td bgcolor= ".$color.">" . htmlspecialchars($cell) . "</td>";
}
}
echo "<tr>\n";
switch($color){
case "#EEEEEE":
$color = "#FFFFFF";
break;
case "#FFFFFF":
$color = "#DDFFCC";
break;
case "#DDFFCC":
$color = "#FFFFFF";
break;
}
}
fclose($f);
echo "\n</table></body></html>";
EDIT:
I would also like to know how to define the colors with meaningful names to make the code more readable
To check whether $color is set to “#EEEEEE”, you need to use a double equals (==) in your if statement, similar to C/C++/C#. Otherwise, you’re just telling PHP to set $color to “#EEEEEE”.