我有一张有以下数据的表格:
______________________
| Name | Date |
----------------------
| Bob | 2016-09-16 |
| Ben | 2016-10-03 |
| Sam | 2016-10-03 |
使用以下方法:
SELECT
case
when datediff( CURDATE(), `the_date`) = 3 then '3 Days'
when datediff( CURDATE(), `the_date`) between 4 and 6 then '4-6 Days'
when datediff( CURDATE(), `the_date`) > 6 then '7 or more days'
end as days,
sum( case
when datediff( CURDATE(), `the_date`) <= 3 then 1
when datediff( CURDATE(), `the_date`) between 4 and 6 then 1
when datediff( CURDATE(), `the_date`) > 6 then 1
else 0
end ) as tot
FROM my_table
GROUP BY
case
when datediff( CURDATE(), `the_date`) <= 3 then '3 Days'
when datediff( CURDATE(), `the_date`) between 4 and 6 then '4-6 Days'
when datediff( CURDATE(),`the_date`) > 6 then '7 or more days'
end ;
然后使用
echo '<p>'.$row['tot'].'</p>';
我得到以下结果,将我的行排序为行是否少于3天、4至6天,还是超过7天。
1
2
理想情况下,我希望对p标记应用一个类,比如class=“紧急”(紧急),用于那些年龄在7天以上,橙色4到6天,然后绿色最多3天的标签。然后,我想用独特的文本包装数字,例如“您迫切需要回复结果应用程序”。
发布于 2016-10-03 19:57:49
我会测试数据结果的值,并有条件地分配一个类名取决于这个值。
if ($row['days'] == '7 or more days') {
$css_class = 'urgent';
} else {
$css_class = 'normal';
}
printf("<p class='%s'>%s</p>", $css_class, $row['tot']);
我不建议在查询中硬编码css类名。不要将数据库查询与表示层混为一谈,这只会在代码中造成混乱和不适当的耦合。
发布于 2016-10-03 19:58:40
可以在查询中添加列(My_class),并将结果添加到p标记中。
SELECT
case
when datediff( CURDATE(), `the_date`) = 3 then '3 Days'
when datediff( CURDATE(), `the_date`) between 4 and 6 then '4-6 Days'
when datediff( CURDATE(), `the_date`) > 6 then '7 or more days'
end as days,
sum( case
when datediff( CURDATE(), `the_date`) <= 3 then 1
when datediff( CURDATE(), `the_date`) between 4 and 6 then 1
when datediff( CURDATE(), `the_date`) > 6 then 1
else 0
end ) as tot ,
case
when datediff( CURDATE(), `the_date`) = 3 then 'green'
when datediff( CURDATE(), `the_date`) between 4 and 6 then 'orange'
when datediff( CURDATE(), `the_date`) > 6 then 'red'
end as my_class
FROM my_table
GROUP BY
case
when datediff( CURDATE(), `the_date`) <= 3 then '3 Days'
when datediff( CURDATE(), `the_date`) between 4 and 6 then '4-6 Days'
when datediff( CURDATE(),`the_date`) > 6 then '7 or more days'
end ;
echo '<p class="'. $row['my_class'] . '" >'.$row['tot'].'</p>'; your query with the proper class a
https://stackoverflow.com/questions/39839351
复制相似问题