繁体   English   中英

基于PHP的基于值的颜色表单元格

[英]Colour Table Cell Based on Value with PHP

我从mysql查询生成一个html表,并希望根据.$result->Games.的值为单元格着色.$result->Games. 出于争论的考虑,如果游戏的值小于10,我希望该单元格为红色,如果大于40,我希望该单元格为绿色。 我还希望能够将10到40之间的任何颜色着色为黄色。

已经有一些问题了,但是我无法修改任何答案以适合此应用程序。 我以为我可能必须使用if函数,并将其与bgcolor="#008000"耦合,例如,但我不确定如何将其合并到下面的代码段中。

$select = $_POST['correlation'] ?: '447';

$myQuery = $wpdb->get_results('SELECT * FROM ' . 'afl_player_correlations' . ' WHERE Player_ID = '. $select . ' AND COV >= 0 ORDER BY ' . COV . ' DESC');

if($myQuery){
echo '<div style="overflow-x:auto;">';
echo '<table class="splits">';
    echo "<tr>"; 
    echo "<th>Teammate</th>"; 
    echo "<th>Games</th>"; 
    echo "<th>Co-Variance</th>"; 
    echo "</tr>"; 
foreach ( $myQuery as $result ) 
    {
        echo '<tr><td>'.$result->Teammate.'</td><td>'.$result->Games.'</td><td>'.$result->COV.'</td></tr>';
    }
echo '</table>';
echo '</div>';
}
foreach ( $myQuery as $result ) 
{
    $style = "background-color: ";
    if ($result->Games < 10) {
        $style .= "red;";
    }
    else if ($result->Games < 40) {
        $style .= "yellow;";
    }
    else{
        $style .= "green;";
    }
    echo '<tr><td>'.$result->Teammate.'</td><td style="$style">'.$result->Games.'</td><td>'.$result->COV.'</td></tr>';
}
<?php
$select = $_POST['correlation'] ?: '447';

$myQuery = $wpdb->get_results('SELECT * FROM ' . 'afl_player_correlations' . ' WHERE Player_ID = '. $select . ' AND COV >= 0 ORDER BY ' . COV . ' DESC');

if($myQuery){
?>
<div style="overflow-x:auto;">
   <table class="splits">
    <tr>
       <th>Teammate</th>
       <th>Games</th>
       <th>Co-Variance</th>
    </tr>"; 
    <?php
    foreach ( $myQuery as $result ) 
    {
       if( $result->Games < 10 ){
          $color = 'red';
       }else if( $result->Games > 40 ){
          $color = 'green';
       }else{
          $color = 'yellow';
       }
    ?>
        <tr>
           <td><?php echo $result->Teammate;?></td>
           <td style="background-color: <?php echo $color;?>"><?php echo $result->Games;?></td>
           <td><?php echo $result->COV;?></td>
        </tr>
    <?php
    }
    ?>
</table>
</div>
<?php
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM