在php中按条件设置颜色

在php中按条件设置颜色,php,html,mysql,Php,Html,Mysql,如何根据条件设置颜色,例如: 在我的表中有一个名为STATUS的列,其中接收来自db的结果。 有三个文本:PG、PEN、ATR 如果状态为PG,则颜色为绿色,笔颜色为红色等 怎么做 我当前的代码是在HTML显示上设置一个类,您甚至可以将其设置为class=“PG”,例如,如果您以后决定更改其颜色。然后在css中定义该类的颜色 使用您提供的示例代码: <th width="4%" scope="col" class="<?php echo $status;?>"> <

如何根据条件设置颜色,例如:

在我的表中有一个名为STATUS的列,其中接收来自db的结果。 有三个文本:PG、PEN、ATR

如果状态为PG,则颜色为绿色,笔颜色为红色等

怎么做


我当前的代码是

在HTML显示上设置一个类,您甚至可以将其设置为
class=“PG”
,例如,如果您以后决定更改其颜色。然后在css中定义该类的颜色

使用您提供的示例代码:

<th width="4%" scope="col" class="<?php echo $status;?>"> <?php echo $status;?></th>

注意:不确定您所说的颜色是指背景色还是文本颜色;这两种方法都有效。

您可以在数组中设置颜色,并可以按如下方式轻松分配颜色

 $color = array (
        "PG"=>"green", // use color code here
        "PEN"=>"red"
       );
在html中,可以从数组调用状态颜色:

<tr style='background-color:<?php echo $color[$row["status"]];?>'>
<td>....</td>

</tr>

有三种方法可以做到这一点(可能还有其他几种),但这里是我的三种方法:设置样式值、重新布线整个文本或内联php颜色设置。。。在我看来,以下是三种方法,从最好的到最坏的

设置样式方法的值
$style = '';

if ( $status == 'PG' )
    $style = 'style="color: green;"'; // You can replace with class=""
else if ( $status == 'PEN' )
    $style = 'style="color: red;"'; // You can replace with class=""
else if ( $status == 'ART')
    $style = 'style="color: blue;"'; // You can replace with class=""

?>

<th width="4%" scope="col" <?=$style?> ><?=$status?></th>
$style='';
如果($status=='PG')
$style='style=“color:green;”“;//您可以替换为class=“”
else if($status=='PEN')
$style='style=“color:red;”“;//您可以替换为class=“”
else如果($status=='ART')
$style='style=“color:blue;”“;//您可以替换为class=“”
?>
重写整行方法

$style = '';

if ( $status == 'PG' )
    $style = 'style="color: green;"'; // You can replace with class=""
else if ( $status == 'PEN' )
    $style = 'style="color: red;"'; // You can replace with class=""
else if ( $status == 'ART')
    $style = 'style="color: blue;"'; // You can replace with class=""

?>

<th width="4%" scope="col" <?=$style?> ><?=$status?></th>
<?php

if ( $status == 'PG' )
    echo '<th width="4%" scope="col" style="color: green;">'.$status.'</th>';
else if ( $status == 'PEN' )
    echo '<th width="4%" scope="col" style="color: red;">'.$status.'</th>';
else if ( $status == 'ART' )
    echo '<th width="4%" scope="col" style="color: blue;">'.$status.'</th>';

?>

内联编辑

$style = '';

if ( $status == 'PG' )
    $style = 'style="color: green;"'; // You can replace with class=""
else if ( $status == 'PEN' )
    $style = 'style="color: red;"'; // You can replace with class=""
else if ( $status == 'ART')
    $style = 'style="color: blue;"'; // You can replace with class=""

?>

<th width="4%" scope="col" <?=$style?> ><?=$status?></th>
echo '<th width="4%" scope="col" ';

if ( $status == 'PG' )
    echo 'style="color: green;"';
else if ( $status == 'PEN' )
    echo 'style="color: red;"';
else if ( $status == 'ART' )
    echo 'style="color: blue;"';

echo '>'.$status.'</th>';
echo'.$status';

我认为您正在尝试根据$status设置背景色…试试这个

<?php 
If($status=='pg'){
      $color='red';
 }
elseif($status=='pen')
{
     $color='green';
}
elseif($status=='atr')
{
      $color='yellow';
 }
?>
<th width="4%" scope="col" style="background-color:<?php echo $color;?>">
    <?php echo $status;?>
</th>


谢谢你们,这解决了我的问题。这个论坛是最好的!谢谢克里斯,我是说文字颜色。@BlitzKrieg,如果这个答案解决了你的问题,别忘了接受它。更多信息:嗨,克里斯,我使用你的代码,这真的解决了我的问题。非常感谢你对我的帮助。现在澄清它是如何工作的?class=“只是为了理解。回显自变量的名称?非常好!正如我在上面的文本中所说,它只生成
class=“PG”
class=“PEN”
,无论$status的值是什么。