2011-07-07 79 views
0

我對php很新,我有一個表格顯示我們的支持門戶案例。案例設置爲「高,中,低」優先級我希望每行的bgcolor根據案例的優先級顯示不同的顏色。例如High = Red,Low = White,Medium = Yellow。tr bgcolor依賴於行值

以下是我的代碼。我將如何去實施這個?任何建議將不勝感激!

<?php 
       $priority = $val['priority']; 
        switch($priority) 
        { 
         case P3: 
         $prior_value = "Low"; 
         break; 
         case P2: 
         $prior_value = "Medium"; 
         break; 
         case P1: 
         $prior_value = "High"; 
        } 
?> 

<tr height="30px"> 
    <Td class="gridcontentredmid" style="width:65px;"><a href="showcase.php?case_id=<?=$val['id']?>"> 
     <?=$val['case_number']?> 
     </a></Td> 
    <Td class="gridcontentmid" style="width:270px; text-align:left;">&nbsp; 
     <?=$val['name']?></Td> 
    <Td class="gridcontentmid" style="width:85px;" ><?=$val['status']?></Td> 
    <Td class="gridcontentmid"><?=$name?></Td> 
    <Td class="gridcontentmid" style="width:65px;"><?=$prior_value?></Td> 
    <Td class="gridcontentmidd" style="border-right:#000 1px solid"><?=$date_create?></Td> 
    </tr> 
+1

我認爲P3是一個字符串不是一個常數? – powtac

回答

0

只需添加一個$color - 變量爲您的switch,然後可將其插入TRclassstyle屬性中。

switch($priority) 
{ 
    case P3: 
     $prior_value = "Low"; 
     $bgColor = '#fff'; 
     break; 
    case P2: 
     $prior_value = "Medium"; 
     $bgColor = '#ff0'; 
     break; 
    case P1: 
     $prior_value = "High"; 
     $bgColor = '#f00'; 
} 

...

<tr style="background-color: <?php echo $bgColor; ?>;"> 
+0

謝謝feeela ..像冠軍一樣工作。我曾嘗試過,但我無法找到<?php echo $ bgColor; ?>事情。 – ipengineer

+0

@ipengineer:正如@minitech所說,你最好應該使用CSS類而不是inline-CSS(請參閱他/她的回答)。 – feeela

+0

完成。切換到CSS。很棒! – ipengineer

3

下面是一個簡單的方法:

   $priority = $val['priority']; 
        switch($priority) 
        { 
         case P3: 
         $prior_value = "Low"; 
         break; 
         case P2: 
         $prior_value = "Medium"; 
         break; 
         case P1: 
         $prior_value = "High"; 
        } 
        $colors = array("High" => "red", "Medium" => "yellow", "Low" => "white"); $color = $colors[$prior_value]; 


<tr style="background-color: <?php echo $color; ?>" height="30px"> 
    <Td class="gridcontentredmid" style="width:65px;"><a href="showcase.php?case_id=<?=$val['id']?>"> 
     <?=$val['case_number']?> 
     </a></Td> 
    <Td class="gridcontentmid" style="width:270px; text-align:left;">&nbsp; 
     <?=$val['name']?></Td> 
    <Td class="gridcontentmid" style="width:85px;" ><?=$val['status']?></Td> 
    <Td class="gridcontentmid"><?=$name?></Td> 
    <Td class="gridcontentmid" style="width:65px;"><?=$prior_value?></Td> 
    <Td class="gridcontentmidd" style="border-right:#000 1px solid"><?=$date_create?></Td> 
    </tr>

但更好的方法是使用CSS:

.priorityHigh { 
    background-color: red; 
} 

.priorityMedium { 
    background-color: yellow; 
} 

.priorityLow { 
    background-color: white; 
} 

   $priority = $val['priority']; 
        switch($priority) 
        { 
         case P3: 
         $prior_value = "Low"; 
         break; 
         case P2: 
         $prior_value = "Medium"; 
         break; 
         case P1: 
         $prior_value = "High"; 
        } 


<tr class="priority<?php echo $prior_value; ?>" height="30px"> 
    <Td class="gridcontentredmid" style="width:65px;"><a href="showcase.php?case_id=<?=$val['id']?>"> 
     <?=$val['case_number']?> 
     </a></Td> 
    <Td class="gridcontentmid" style="width:270px; text-align:left;">&nbsp; 
     <?=$val['name']?></Td> 
    <Td class="gridcontentmid" style="width:85px;" ><?=$val['status']?></Td> 
    <Td class="gridcontentmid"><?=$name?></Td> 
    <Td class="gridcontentmid" style="width:65px;"><?=$prior_value?></Td> 
    <Td class="gridcontentmidd" style="border-right:#000 1px solid"><?=$date_create?></Td> 
    </tr>