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:
<td style="background-color: <?php echo $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
?>
<td style="background-color:#FF0;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?
} elseif($f3==1) {
// Display YELLOW
?>
<td style="background-color:#0F0;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?
} else {
?>
<td style="background-color:#F00;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?
}
?>
for these:
<?
switch($f3) {
case 2: $color="#F00"; break; // Display RED
case 1: $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:
<table style="width:324px;" border="3" cellspacing="1" cellpadding="1">
<tr>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Task</font></b>
</td>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">deadline</font></b>
</td>
<td style="background-color:#A4A4A4;">
<b><font face="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");
?>
<tr style="background-color:#A4A4A4;">
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?
if($f3==2) {
// Display RED
?>
<td style="background-color:#FF0;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?
} elseif($f3==1) {
// Display YELLOW
?>
<td style="background-color:#0F0;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?
} ?>
<?php if($f3==2){?>
<td style="background-color:#F00;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<? }else { ?>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?php } ?>
</tr>
<?php
$i++;
}
?>
</table>
Solution 4:
There is a mistake in the columns count if you see. Try this way:
<table style="width:324px;" border="3" cellspacing="1" cellpadding="1">
<tr>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Task</font></b>
</td>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">deadline</font></b>
</td>
<td style="background-color:#A4A4A4;">
<b><font face="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");
?>
<tr style="background-color:#A4A4A4;">
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<?
if($f3==2) {
// Display RED
?>
<td style="background-color:#FF0;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?
} elseif($f3==1) {
// Display YELLOW
?>
<td style="background-color:#0F0;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?
} else {
?>
<td style="background-color:#F00;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?
}
?>
</tr>
<?php
$i++;
}
?>
</table>
Post a Comment for "How Change Cell Table Color In Php Based On Cell Value?"