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 it has.
I was thinking this way, but the values are repeated and the color is not the right way.
Can anyone help me please?
Did you understand my question?
Thank you all. I apologize for the inconvenience.
Code:
<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>
<?
}
else
{
?>
<td style="background-color:#F00;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<?
}
?>
</tr>
<?php
$i++;
}
?>
</table>
Result from code:
SOLVED
Thanks everyone for your help. Status is a predefined value recorded in the database. Only ranges between 1 and 4 so that the resolution has served @Daniel Andre correctly. All responses were valid for me to try to understand a little more php, but it was so simple. Again thank you all for your time and for your help.
The final result was:
<table style="width:324px;" border="3" cellspacing="1" cellpadding="1">
<tr>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Tarefa</font></b>
</td>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Prazo</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,"idTarefa");
$f2=mysql_result($result,$i,"PrazoExecucao");
$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>-->
<?php
$status_colors = array(1 => '#0000FF', 2 => '#00FF00', 3 => '#FFFF00', 4 => '#FF0000');
?>
<td style="background-color: <?php echo $status_colors[$f3]; ?>;">
</tr>
<?php
$i++;
}
?>
</table>
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]; ?>;">
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments