php查询-背景颜色更改

php查询-背景颜色更改,php,html,Php,Html,我想要的是根据查询返回的值更改表中单元格的颜色 我所做的就是这样- 得体 .priority_1, priority_-1, priority_0{ background-color: green; color:green; } .priority_4, .priority_5, .priority_6, .priority_7, .priority_-4, .priority_-5, .priority_-6, .priority_-7{ ba

我想要的是根据查询返回的值更改表中单元格的颜色

我所做的就是这样- 得体

 .priority_1, priority_-1, priority_0{
    background-color: green;
    color:green;
    }
    .priority_4, .priority_5, .priority_6, .priority_7, .priority_-4, .priority_-5, .priority_-6, .priority_-7{
        background-color: red;
        color:red;
}
在身体细胞里

<?php
$result = mysqli_query($con,"SELECT SHOP, FORMAT(VARMP,0) AS value FROM recordstable WHERE SHOP='1' AND Month='1' AND Type='TCheck'");

while($row = mysqli_fetch_array($result)) {


    $priority = $row['value'];

echo "<td class=\"priority_{$priority}\"><center>";
echo $priority . "";
}
?>
</td>

这给了我想要的,但是,如果该值超出范围,该怎么办?如果我得到的值是43,我希望它是红色的。但是.优先级只会在7到7之间变为红色。我如何做一系列简单的风格。不做优先级1-100加一减。

不需要创建那么多类。如果我理解正确,您可能需要以下内容:

CSS:

PHP:


我建议动态应用一个类,让样式由CSS决定。背景颜色不需要在CSS中设置吗?只是好奇,while循环应该在元素内部,还是应该为每个返回的结果生成一个新元素。我这样问的原因是,如果所有结果都在元素中,则必须单独包装返回的结果以更改其颜色。否则将有一个颜色,绿色或红色…有人想扩大一点以上的评论?
.priority_green{
    background-color: green;
    color:green;
}

.priority_red{
        background-color: red;
        color:red;
}
while($row = mysqli_fetch_array($result)) {

    $priority = $row['value'];

    $class = 'green';
    if($priority >= -7 and $priority <= 7){ $class = 'red'; } //from 7 to -7 only

    echo "<td class=\"priority_$class\"><center>";
    echo $priority . "";
    //don't forget to close <center> and <td> and you also don't have rows - just a note
}