dougekui1518 2014-12-04 20:04
浏览 77
已采纳

PHP:根据结果设置表格单元格背景

I'm trying to generate colours based on output of a csv (originally formatted ping).

I can get the values no problem,but the if, ifelse, else doesn't seem to be working.

if($min > 0.499 && $min <= 1) {$tcolor = $yellow;} elseif($min >= 1.0) {$tcolor = $red; } else { $tcolor = $white;}
if($avg > 0.499 && $avg <= 1) {$tcolor = $yellow;} elseif($avg >= 1.0) {$tcolor = $red; } else { $tcolor = $white;}
if($max > 0.499 && $max <= 1) {$tcolor = $yellow;} elseif($max >= 1.0) {$tcolor = $red; } else { $tcolor = $white;}
if($mdev > 0.499 && $mdev <= 1) {$tcolor = $yellow;} elseif($mdev >= 1) {$tcolor = $red; } else { $tcolor = $white;}

echo "<tr><td>$ip</td><td bgcolor=\"$tcolor\">$min<br>$tcolor</td><td bgcolor=\"$tcolor\">$avg<br>$tcolor</td><td bgcolor=\"$tcolor\">$max<br>$tcolor</td><td bgcolor=\"$tcolor\">$mdev</td></tr>";

Edit: As many asked about the colour code already, I have it above the code listed as

$yellow = "#FFFF66";
$red = "#FF0000";
$white = "#FFFFFF";

And the number I see the overlap, but I've also tried with 0.999 with the same result.

  • 写回答

2条回答 默认 最新

  • douxuexiao1748 2014-12-04 20:21
    关注

    It sounds like you got it solved, but you should look at using functions like so, so you don't have to repeat if statements like you did.

    <?php
    
        function setCellColor($value){
    
            $color = '#FFFFFF';
    
            if($value >= 0.5 && $value <= 1){
                $color = '#FFFF00';
            } else if($value > 1) {
                $color = '#FF0000';
            }
    
            return ' style="background: ' . $color . '" ';
        }
    
        echo '<tr><td>' . $ip . '</td><td ' . setCellColor($min) . '>' . $min . '</td><td ' . setCellColor($avg) . '>' . $avg . '</td><td ' . setCellColor($max) . '>' . $max . '</td><td ' . setCellColor($mdev) . '>' . $mdev . '</td></tr>';
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab中mjs用不了
  • ¥15 Ios抖音直播的时候如何添加自定义图片在直播间!
  • ¥60 riscv-pulpino总线上挂载axi从机
  • ¥15 ssh登录页面的问题
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥15 stm32 串口通讯过程中的问题
  • ¥20 公众号如何实现点击超链接后自动发送文字