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

时间:2018-07-22 10:58:54

标签: php html

我从mysql查询生成一个html表,并希望根据.$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>';
}

2 个答案:

答案 0 :(得分:0)

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>';
}

答案 1 :(得分:0)

<?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
}
?>