Home  >  Article  >  Backend Development  >  PHP如何取查询结果中的最大值并赋予style

PHP如何取查询结果中的最大值并赋予style

WBOY
WBOYOriginal
2016-06-23 13:40:04958browse

现从数据库X表中取出a,b,time三个字段的值。
如果查询结果中a的值为小明的话,则背景色为#96,字体颜色为白色。
现需要再加一个条件,如果time(类型为datetime)为距离现在最近的时间,则该行所有字段的字体变为红色。
请问如何设置$time_style。
同时满足以上两个条件。
以下代码为满足第一个条件的:

$sql = "select a,b,time from X"; 
$rst = mysql_query($sql); 
while($row=mysql_fetch_assoc($rst)){ 
$vip_style = $row['a']=="小明" ? ' style=" background-color:#096;color:white;"' : '';
echo "

{$row['a']} {$row['b']} {$row['time']} ";}


回复讨论(解决方案)

你可以 $sql = "select a,b,time from X order by time desc;
取第一条 数据 ;如果这条数据满足a==小明 ,就变红 。这样不就满足两个条件了 

必须先计算出时间差,然后才能知道谁最小
你想一遍循环就完成,是做不到的

你可以 $sql = "select a,b,time from X order by time desc;
取第一条 数据 ;如果这条数据满足a==小明 ,就变红 。这样不就满足两个条件了 



怎么取?

必须先计算出时间差,然后才能知道谁最小
你想一遍循环就完成,是做不到的



或者说我如何取出最大的time值那一行的所有字段进行style设置?


你可以 $sql = "select a,b,time from X order by time desc;
取第一条 数据 ;如果这条数据满足a==小明 ,就变红 。这样不就满足两个条件了 



怎么取?
$row=mysql_fetch_row[$sql];



你可以 $sql = "select a,b,time from X order by time desc;
取第一条 数据 ;如果这条数据满足a==小明 ,就变红 。这样不就满足两个条件了 



怎么取?
$row=mysql_fetch_row[$sql];



你可以 $sql = "select a,b,time from X order by time desc;
取第一条 数据 ;如果这条数据满足a==小明 ,就变红 。这样不就满足两个条件了 



怎么取?
$row=mysql_fetch_row[$sql];
不好意思 应该是
$result=mysql_query($sql);
$row=mysql_fetch_row[$result];
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn