0

您好,我有一个包含一些字段的表格,例如
在此处输入图像描述

在这里我想为表格整行设置颜色..意味着如果 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>  

提前致谢

4

3 回答 3

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>  
于 2012-09-05T07:17:55.410 回答
0

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

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

于 2012-09-05T07:17:52.930 回答
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>
于 2012-09-05T07:20:38.153 回答