php查询 - 背景颜色更改 [英] php query - Background color changes
问题描述
我想要的是根据查询返回的值改变表格中单元格的颜色。
What I want is to change the color of a cell in a table based on the value that is returned by the query.
我所做的是这样的 - -
在样式中
what I have done is this -- In the style
.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;
}
和正文 - 单元格
and in the body - cell
<?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加上减号。
this gives me what i want however, what if the value falls outside the range - if i get a value of 43 I want it red. but the .priority will only chnage to red for 7 to -7. how can i do a range easy easy style. without doing priority 1 - 100 plus an minus.
推荐答案
无需创建那么多类。如果我理解正确,你可能需要这样的东西:
No need to create that many classes. If I've understood correctly, you probably need something like:
CSS:
.priority_green{
background-color: green;
color:green;
}
.priority_red{
background-color: red;
color:red;
}
PHP: $ b
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
}
这篇关于php查询 - 背景颜色更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!