php查询 - 背景颜色更改

时间:2014-06-16 20:29:46

标签: 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{
        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我想要它是红色的。但.priority只会在7到-7之间变为红色。我怎么能做一个范围轻松简单的风格。没有优先级1 - 100加上减号。

1 个答案:

答案 0 :(得分:0)

无需创建那么多类。如果我理解正确,你可能需要这样的东西:

<强> CSS:

.priority_green{
    background-color: green;
    color:green;
}

.priority_red{
        background-color: red;
        color:red;
}

<强> PHP:

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
}
相关问题