使用PHP根据值动态更改HTML TD背景色

ilmyapht  于 2023-05-16  发布在  PHP
关注(0)|答案(2)|浏览(158)

我试图改变TD的背景颜色dinamically,但它让我疯狂,因为我真的不知道如何修复代码。
这是我的代码:

<td data-column="% Over 0.5 SH" style="background-color: <?php echo $backgroundColorOver05SH; ?>">
<?php 
if (($row['TotalMatch']) > 9){ 
$percover05sh = $row['OK_05sh'] / $row['TotalMatch'] * 100; 
echo sprintf("%.2f", $percover05sh);

if ($percover05sh > 80){
    $backgroundColorOver05SH = "green";
} elseif ($percover05sh >= 70 and $percover05sh <= 79.99  ){
    $backgroundColorOver05SH = "yellow";
} else {
    $backgroundColorOver05SH = "red";
}

我认为我的方向是正确的,但我无法得到正确的解决方案。有什么建议吗?谢谢!

**编辑:**这段代码现在可以工作了!这是我的完整代码:

<?php 
if (($row['TotalMatch']) > 9){ 
$percover05sh = $row['OK_05sh'] / $row['TotalMatch'] * 100; 

if ($percover05sh >= 80){
    $backgroundColorOver05SH = "green";
} elseif ($percover05sh >= 70 && $percover05sh < 80  ){
    $backgroundColorOver05SH = "yellow";
} else {
    $backgroundColorOver05SH = "red";
}

}else{
echo 'No Bet';
}     ?>
<td data-column="% Over 0.5 SH" style="background-color: <?php echo $backgroundColorOver05SH;?>;">
<?php
echo sprintf("%.2f", $percover05sh);    
?>
</td>
rqenqsqc

rqenqsqc1#

首先:

<?php 
if (($row['TotalMatch']) > 9){ 
$percover05sh = $row['OK_05sh'] / $row['TotalMatch'] * 100; 
echo sprintf("%.2f", $percover05sh);

if ($percover05sh > 80){
    $backgroundColorOver05SH = "green";
} elseif ($percover05sh >= 70 && $percover05sh < 80  ){
    $backgroundColorOver05SH = "yellow";
} else {
    $backgroundColorOver05SH = "red";
}
?>
<td data-column="% Over 0.5 SH" style="background-color: <?=$backgroundColorOver05SH;?>;">
<?php
/* make sure to close:
if (($row['TotalMatch']) > 9){ 
*/
}
?>

如果颜色没有改变,可能是由不同的CSS规则或脚本设置的?

8hhllhi2

8hhllhi22#

除了更改代码中的顺序(如问题的注解中所写)之外,我还将在style属性中PHP响应的background-color值之后添加一个分号,即

<td data-column="% Over 0.5 SH" style="background-color: <?php echo $backgroundColorOver05SH; ?>;">

相关问题