Skip to content Skip to sidebar Skip to footer

How Change Cell Table Color In Php Based On Cell Value?

I just started programming in php. I made a table that returns values ​​from the database. I'm trying to do the following: Change the color of the third column by the value i

Solution 1:

If the statuses are static (meaning, they don't change, but are a fixed set of statuses), you can make a php array mapping the statuses to colors:

<?php$status_colors = array(1 => '#FF0', 2 => '#F0F', 3 => '#0FF', 4 => '#0F0');

Then, in your td's, use the correct status color:

<tdstyle="background-color: <?phpecho$status_colors[$f3]; ?>;">

Solution 2:

If I got it right, you are displaying 3 columns (yellow, green, red), and only one should be displayed. So, you could try changing these lines:

<?if($f3==2) { 
    // Display RED ?><tdstyle="background-color:#FF0;"><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><? 
} elseif($f3==1) { 
    // Display YELLOW ?><tdstyle="background-color:#0F0;"><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><?     
} else { 
?><tdstyle="background-color:#F00;"><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><? 
} 
?>

for these:

<?switch($f3) {
    case2: $color="#F00"; break; // Display RED case1: $color="#FF0"; break; // Display YELLOW default:  $color="#0F0"; break; // Display GREEN 
}
echo'<td style="background-color:'.$color.';"><font face="Arial, Helvetica, sans-serif">'.$f3.'</font></td>';
?>

Additional remarks:

  • try to switch to PDO / mysqli (you are using deprecated functions).
  • note that red id #F00, green #0F0, yellow #FF0. These values were not matching the comments in the provided code.

Solution 3:

<tablestyle="width:324px;"border="3"cellspacing="1"cellpadding="1"><tr><tdstyle="background-color:#A4A4A4;"><b><fontface="Arial, Helvetica, sans-serif">Task</font></b></td><tdstyle="background-color:#A4A4A4;"><b><fontface="Arial, Helvetica, sans-serif">deadline</font></b></td><tdstyle="background-color:#A4A4A4;"><b><fontface="Arial, Helvetica, sans-serif">Status</font></b></td></tr><?php$i=0;while ($i < $num) {
$f1=mysql_result($result,$i,"Task");
$f2=mysql_result($result,$i,"deadline");
$f3=mysql_result($result,$i,"status");

?><trstyle="background-color:#A4A4A4;"><td><fontface="Arial, Helvetica, sans-serif"><?phpecho$f1; ?></font></td><td><fontface="Arial, Helvetica, sans-serif"><?phpecho$f2; ?></font></td><td><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><?if($f3==2) { 
    // Display RED ?><tdstyle="background-color:#FF0;"><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><? 
} elseif($f3==1) { 
    // Display YELLOW ?><tdstyle="background-color:#0F0;"><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><?     
} ?><?phpif($f3==2){?><tdstyle="background-color:#F00;"><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><? }else { ?><td><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><?php } ?></tr><?php$i++;
}
?></table>

Solution 4:

There is a mistake in the columns count if you see. Try this way:

<tablestyle="width:324px;"border="3"cellspacing="1"cellpadding="1"><tr><tdstyle="background-color:#A4A4A4;"><b><fontface="Arial, Helvetica, sans-serif">Task</font></b></td><tdstyle="background-color:#A4A4A4;"><b><fontface="Arial, Helvetica, sans-serif">deadline</font></b></td><tdstyle="background-color:#A4A4A4;"><b><fontface="Arial, Helvetica, sans-serif">Status</font></b></td></tr><?php$i=0;
while ($i < $num) {
    $f1=mysql_result($result,$i,"Task");
    $f2=mysql_result($result,$i,"deadline");
    $f3=mysql_result($result,$i,"status");
?><trstyle="background-color:#A4A4A4;"><td><fontface="Arial, Helvetica, sans-serif"><?phpecho$f1; ?></font></td><td><fontface="Arial, Helvetica, sans-serif"><?phpecho$f2; ?></font></td><?if($f3==2) { 
        // Display RED ?><tdstyle="background-color:#FF0;"><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><? 
    } elseif($f3==1) { 
        // Display YELLOW ?><tdstyle="background-color:#0F0;"><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><?     
    } else { 
?><tdstyle="background-color:#F00;"><fontface="Arial, Helvetica, sans-serif"><?phpecho$f3; ?></font></td><? 
    } 
?></tr><?php$i++;
}
?></table>

Post a Comment for "How Change Cell Table Color In Php Based On Cell Value?"