如何给表tr赋予颜色?

时间:2012-09-05 07:07:06

标签: php css

您好我有一个表格,其中包含一些字段 enter image description here

这里我想要为整个表格制作颜色..如果ASR值是75到100应该得到一种颜色,50到75应该得到另一种颜色,低于50应该得到另一种颜色。

这是我的PHP代码

<table width="75%" border="1">
<tr>
<td align="center">channel no</td>
<td align="center">IP</td>
<td align="center">Total calls</td>
<td align="center">Connected calls</td>
<td align="center">Disconnected calls</td>
<td align="center">Duration</td>
<td align="center">ASR</td>
<td align="center">ACD</td>

</tr> 
<?php

while ($row = mysql_fetch_assoc($result)) {

//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr>

<td>".$row['channel']."&nbsp;</td>
<td>".$row['ip']."&nbsp;</td>
<td>".$row['totalcalls']."&nbsp;</td>";

if ($row['totalcalls']>1){
$sql1    = "SELECT count(duration) as count  FROM gateways where duration=0 and ip='".$_POST['ip']."' and channel='".$row['channel']. "' and (connect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."'  or disconnect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' ) Group by channel";

$result1 = mysql_query($sql1, $link);
$norow=mysql_fetch_assoc($result1);
$attenedcalls=($row['totalcalls']-$norow['count']);
echo "<td>".$attenedcalls."&nbsp;</td>";
$disconnectedcalls=($row['totalcalls']-$attenedcalls);
echo "<td>".$disconnectedcalls."&nbsp;</td>";
echo "   <td>".$row['tduration']."&nbsp;</td>";
echo "<td>".(($attenedcalls/$row['totalcalls'])*100)."</td>";
}else{

    echo "<td>".$row['totalcalls']."</td>";

    echo "<td>100</td>";

}

$minutes = gmdate("H:i:s", ($row['tduration']/$attenedcalls));
echo "   <td>".$minutes."&nbsp;</td>
</tr>";
}
?>
</table>  

提前致谢

3 个答案:

答案 0 :(得分:1)

您可以尝试这样

<table width="75%" border="1">
<tr>
<td align="center">channel no</td>
<td align="center">IP</td>
<td align="center">Total calls</td>
<td align="center">Connected calls</td>
<td align="center">Disconnected calls</td>
<td align="center">Duration</td>
<td align="center">ASR</td>
<td align="center">ACD</td>

</tr> 
<?php

while ($row = mysql_fetch_assoc($result)) {
$color = '';
if ($row['totalcalls']>1){
    $sql1    = "SELECT count(duration) as count  FROM gateways where duration=0 and ip='".$_POST['ip']."' and channel='".$row['channel']. "' and (connect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."'  or disconnect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' ) Group by channel";

    $result1 = mysql_query($sql1, $link);
    $norow=mysql_fetch_assoc($result1);
    $attenedcalls=($row['totalcalls']-$norow['count']);
    $asr = (($attenedcalls/$row['totalcalls'])*100);
    if($asr >= 75 && $asr <=100 ){
        $color = 'red';
    }else if($asr >= 50 && $asr < 75){
        $color = 'cyan';
    }else if($asr < 50){
        $color = 'blue';
    }
}
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr style='background-color : ".$color."'>

<td>".$row['channel']."&nbsp;</td>
<td>".$row['ip']."&nbsp;</td>
<td>".$row['totalcalls']."&nbsp;</td>";

if ($row['totalcalls']>1){

echo "<td>".$attenedcalls."&nbsp;</td>";
$disconnectedcalls=($row['totalcalls']-$attenedcalls);
echo "<td>".$disconnectedcalls."&nbsp;</td>";
echo "   <td>".$row['tduration']."&nbsp;</td>";
echo "<td>".$asr."</td>";
}else{

    echo "<td>".$row['totalcalls']."</td>";

    echo "<td>100</td>";

}

$minutes = gmdate("H:i:s", ($row['tduration']/$attenedcalls));
echo "   <td>".$minutes."&nbsp;</td>
</tr>";
}
?>
</table>  

答案 1 :(得分:0)

修改while循环,以便在发出<tr>标记之前计算ASR值。使用该值根据您设置的分类选择一个类,并发出<tr class=foo>形式的标记,其中foo是您选择的类名。然后,只需使用tr.foo等类选择器为类编写CSS规则。

(前提是您没有在td单元格上设置颜色。如果有,您需要使用tr.foo td之类的选择器来覆盖此类设置。)

答案 2 :(得分:0)

 [...]  
 while ($row = mysql_fetch_assoc($result)) { 
 $asrVal=(($attenedcalls/$row['totalcalls'])*100);
 if($asrVal>=50 && $asrVal <=75) $class="from50to75";
 if($asrVal>=75 && $asrVal <=100) $class="from75to100";
 if($asrVal<50) $class="below50";
 //$minutes = gmdate("H:i:s", $row['tduration']); 
 echo "<tr class='$class'> 
 [...]  

然后添加:

<style>
tr.from50to75 td{background-color:red;}
tr.from75to100 td{background-color:green;}
tr.below50 td{background-color:blue;}
</style>