首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对不同日期的结果应用不同的类

对不同日期的结果应用不同的类
EN

Stack Overflow用户
提问于 2016-10-03 19:52:01
回答 2查看 28关注 0票数 2

我有一张有以下数据的表格:

代码语言:javascript
运行
复制
 ______________________
| Name |    Date       |
 ----------------------
| Bob  |  2016-09-16   |
| Ben  |  2016-10-03   |
| Sam  |  2016-10-03   |

使用以下方法:

代码语言:javascript
运行
复制
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 ;   

然后使用

代码语言:javascript
运行
复制
echo '<p>'.$row['tot'].'</p>';

我得到以下结果,将我的行排序为行是否少于3天、4至6天,还是超过7天。

代码语言:javascript
运行
复制
1
2

理想情况下,我希望对p标记应用一个类,比如class=“紧急”(紧急),用于那些年龄在7天以上,橙色4到6天,然后绿色最多3天的标签。然后,我想用独特的文本包装数字,例如“您迫切需要回复结果应用程序”。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-03 19:57:49

我会测试数据结果的值,并有条件地分配一个类名取决于这个值。

代码语言:javascript
运行
复制
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类名。不要将数据库查询与表示层混为一谈,这只会在代码中造成混乱和不适当的耦合。

票数 2
EN

Stack Overflow用户

发布于 2016-10-03 19:58:40

可以在查询中添加列(My_class),并将结果添加到p标记中。

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39839351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档