简体   繁体   English

PHP-在表格行中添加特定的背景色

[英]PHP - Add specific background color in table row

I have a table which has the following structure: 我有一个具有以下结构的表:

+--------+---------+----------+
|Col1    | Col2      | Col3   |
+-----------------------------+
| BRA1   | QI        | QI     |
+--------+-----------+--------+
| BRA2   | Validated | QI     |
+-----------------------------+

I would like to higlight cell in red when it is equal to 'QI' and in green color when it is equal to 'Validated' . 我想用红色表示等于'QI'单元格,而用绿色表示等于'Validated'

This is what I did, but it doesn't work. 这是我所做的,但是没有用。 Can any anyone could help to fix my script? 任何人都可以帮助修复我的脚本吗?

My php script: 我的PHP脚本:

<?php

$fields_num = mysqli_num_fields($result);
echo '<h1>Table:'.$tablename.'</h1>';
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysqli_fetch_fields($result);
echo '<td>'.$field[$i]->name.'</td>';
}
echo "</tr>\n";
// printing table rows
while($row = mysqli_fetch_row($result))
{
echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
    if($cell=="QI"){
        //echo "<td>$cell</td>";
        echo '<td BGCOLOR="#ff0000">'.$cell.'</td>';}
        elseif ($cell=="Validated") {echo '<td BGCOLOR="#3f9a0e">'.$cell.'</td>';} //Green color BGCOLOR="#3f9a0e"

    echo "</tr>\n";
}
mysqli_free_result($result);


?>

您需要样式属性并更改背景颜色

<td style="background-color: #ffffff">...</td>

Try this code: 试试这个代码:

<?php
...
$fields_num = mysqli_num_fields($result);
echo '<h1>Table:'.$tablename.'</h1>';
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysqli_fetch_fields($result);
echo '<td>'.$field[$i]->name.'</td>';
}
echo "</tr>\n";
// printing table rows
while($row = mysqli_fetch_row($result))
{
echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
foreach($row as $cell) {
    if($cell=="QI"){

            echo '<td style="background:#ff0000">'.$cell.'</td>';

    } elseif ($cell=="Validated") {

            echo '<td style="background:#3f9a0e">'.$cell.'</td>';
    }
}

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

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