如何根据单元格值更改php中的单元格表颜色? [英] How change cell table color in php based on cell value?
问题描述
我刚刚开始用php编程.
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.
有人可以帮我吗?
您了解我的问题吗?
谢谢大家.不便之处,敬请见谅.
Thank you all. I apologize for the inconvenience.
代码:
<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>
代码结果
已解决
感谢大家的帮助.状态是记录在数据库中的预定义值.只有介于1到4之间的范围,这样分辨率才能正确提供@Daniel Andre.对于我来说,尝试理解更多的php都是有效的,但是它是如此的简单.再次感谢大家的时间和帮助.
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.
最终结果是:
<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>
推荐答案
如果状态是静态的(意味着它们没有变化,但是是一组固定的状态),则可以制作一个将状态映射到的php数组.颜色:
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');
然后,在您的td中,使用正确的状态颜色:
Then, in your td's, use the correct status color:
<td style="background-color: <?php echo $status_colors[$f3]; ?>;">
这篇关于如何根据单元格值更改php中的单元格表颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!