我想要做的是,我想突出显示(黄色)哪一行的每 1K 使用量高于平均值。结果应该是这样的。
这是我的代码,用于在超过平均值时突出显示一行,但我无法将值 if($row['duh']>'0.77')
更改为 if($row['duh']>'$avg')
因为它会突出显示所有行。
$no = 1;
while ($row = mysqli_fetch_array($result))
{
$month1=strtotime($row['month']);
$month=date('m-Y',$month1);
$store_name=$row['store_name'];
$netamt1=$row['netamt'];
$netamt11=number_format($netamt1,2);
$usage1=$row['monusage'];
$usage11=number_format($usage1,2);
$answer=$usage1/$netamt1*1000;
$answer1=number_format($answer,2);
$total+=$answer1;
$avg=$total/$no;
$duh = number_format($row['duh'],2);
echo "<tr>";
echo "<td>".$no."</td>";
echo "<td>".$store_name."</td>";
echo "<td>".$date2111."</td>";
echo "<td>".$netamt11."</td>";
echo "<td>".$usage11."</td>";
if($row['duh']>'0.77') {// 0.77 i cannot change to $avg, because it will hightlight all the rows become yellow
echo "<td style='background-color: #FFFF00;'>".$duh."</td>";}
else {
echo "<td>".$duh."</td>";
}
echo "</tr>";
$no++;
}?>
</tbody>
<tfoot>
<tr>
<th colspan="5">TOTAL</th>
<th><?=($total)?></th>
</tr>
<tr>
<th colspan="5">AVERAGE</th>
<th><?=number_format($avg,4)?></th>
</tr>
</tfoot>
最佳答案
您可以将 average
添加为查询结果中的另一列,并将值与其进行比较,例如:
SELECT no, outlet, date, sales, usage, usage_per_1k, AVG(usage_per_1k) AS avg
FROM table
WHERE condition;
然后,添加以下内容以突出显示:
if($row['duh']>$row['avg']) {
hightlight all the rows become yellow
echo "<td style='background-color: #FFFF00;'>".$duh."</td>";
}
关于mysql - 突出显示表 html 中的一行,该行的值超过平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45028481/