tr bgcolor dependant on row value
I am very new to php and I have a table that displays cases from our support portal. Cases are set to a priority of "High, Medium, or Low" I want the bgcolor of each row to display a different color based on the priority of that case. For exacmple High = Red, Low = White, Medium = Yellow.
Below is the code I have. How would I go about implementing this? Any suggestions would be greatly appreciated!
<?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;">&l开发者_C百科t;a href="showcase.php?case_id=<?=$val['id']?>">
<?=$val['case_number']?>
</a></Td>
<Td class="gridcontentmid" style="width:270px; text-align:left;">
<?=$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>
Here's a quick way:
$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;">
<?=$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>
But a much better way would be to use CSS:
.priorityHigh {
background-color: red;
}
.priorityMedium {
background-color: yellow;
}
.priorityLow {
background-color: white;
}
and
$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;">
<?=$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>
Just add a $color
-variable to your switch
, which then can be inserted in the TR
s class
or style
attribute.
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; ?>;">
精彩评论