tr bgcolor依赖于行值

时间:2011-07-07 23:50:49

标签: php mysql

我是php的新手,我有一个表格,可以从我们的支持门户网站显示案例。案例设置为“高,中或低”的优先级我希望每行的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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

只需将$color - 变量添加到switch,然后可以将其插入TR s classstyle属性。

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; ?>;">