2012-09-05 62 views
0

你好,我有一些字段的表像
enter image description here如何給table tr添色彩?

在這裏我要做出表整個rows..means的顏色,如果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> 

感謝

+2

你應該看看CSS,你能給我們e可以輕鬆設置顏色。所有你需要做的就是爲每一行設置一個樣式,CSS將完成剩下的工作。 – Fluffeh

+1

你的第一排應該是'頻道沒有' – Dementic

回答

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> 
+0

謝謝Poonam .. :-) – kiran

0

修改您while循環,讓你計算髮射<tr>標籤前的ASR值。使用該值根據您設置的分類選擇一個班級,併發出表格<tr class=foo>的標籤,其中foo是您選擇的班級名稱。然後,這只是爲類編寫CSS規則的問題,使用類選擇器如tr.foo

(前提是你沒有設置對td細胞的顏色。如果你有,你需要使用選擇像tr.foo td來覆蓋這些設置。)

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>