php - tr bgcolor 取决于行值

标签 php mysql

我对 php 非常陌生,我有一个表格显示来 self 们支持门户的案例。案例的优先级设置为“高、中或低”,我希望每行的背景颜色根据该案例的优先级显示不同的颜色。例如,高 = 红色,低 = 白色,中 = 黄色。

下面是我的代码。我将如何实现这个?任何建议将不胜感激!

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

最佳答案

这是一个快速方法:

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


 <tr <b>style="background-color: <?php echo $color; ?>"</b> 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 <b>class="priority<?php echo $prior_value; ?>"</b> 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>

关于php - tr bgcolor 取决于行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6618472/

相关文章:

php - Mysql查询插入多个表相同的id

php - 如何preg_match所有数字而不是url编码的逗号

php - 当您将鼠标悬停在网页上的链接上时,如何隐藏浏览器状态栏上的链接?

javascript - 如何通过填充从数据库获取的数据来自动在 HTML 表中创建行

mysql - 如何计算Mysql中两个日期之间不包括周末(周六和周日)的天数?

mysql - 删除一个字段的重复项 - 分组依据和排序依据

MySQL SELECT 然后删除

php - 无法登录 Php MySQL android

php - 如何在不复制/粘贴身份验证 token 的情况下使用 Google Drive PHP API 进行身份验证

mysql select 同一列出现两次,但where子句不同