2016-06-21 86 views
0

當條件滿足時,如何更改表中整行的顏色?當條件滿足時,更改表中整行的顏色?

例如,返回FAILURE?使整行成爲紅色。

<tr> <!-- Disk Space Available --> 
       <td class="tg-yw4l">ld</td> 
       <td class="tg-yw4l"> 
       <?php 
        if ($ld_status == 0) { 
         echo 'SUCCESS'; 
        } else if ($ld_status == 1) { 
         echo 'WARNING'; 
        } else { 
         echo 'FAILURE'; 
        } 
       ?> 
       </td> 

+0

使用''標籤的bgcolor屬性。 –

+1

@DanBracuk [甚至W3Schools](http://www.w3schools.com/tags/att_tr_bgcolor.asp)(不完全知道在曲線之前)謹慎使用'bgcolor'屬性。 –

回答

3

您可以生成類PHP的TR,那麼在CSS中你可以改變每個類的風格。

CSS

.success{ 
    background-color: green; 
    } 

.warning{ 
    background-color: yellow; 
    } 

.failure{ 
    background-color: red; 
    } 

HTML

<tr class=" <?php 
        if ($ld_status == 0) { 
         echo 'success'; 
        } else if ($ld_status == 1) { 
         echo 'warning'; 
        } else { 
         echo 'failure'; 
        } 
       ?>"> 

       <td class="tg-yw4l">ld</td> 
       <td class="tg-yw4l"> 
       <?php 
        if ($ld_status == 0) { 
         echo 'SUCCESS'; 
        } else if ($ld_status == 1) { 
         echo 'WARNING'; 
        } else { 
         echo 'FAILURE'; 
        } 
       ?> 
       </td> 
0

試試這將是工作的合適你的病情

<html> 
    <body> 
    <?php $ld_status=2; ?> 
    <table border="1"> 
    <tr style="background:<?php if($ld_status == 0) { echo 'green'; } else if ($ld_status == 1){ echo 'yellow'; } else { echo 'red';}?>;" > <!-- Disk Space Available --> 
        <td class="tg-yw4l">ld</td> 
        <td class="tg-yw4l"> 
        <?php 
         if ($ld_status == 0) { 
          echo 'SUCCESS'; 
         } else if ($ld_status == 1) { 
          echo 'WARNING'; 
         } else { 
          echo 'FAILURE'; 
         } 
        ?> 
        </td> 
      </table>   
    </html> 
+0

它沒有工作:( –