PHP - 在 table 行中添加特定的背景颜色
PHP - Add specific background color in table row
我有一个 table 结构如下:
+--------+---------+----------+
|Col1 | Col2 | Col3 |
+-----------------------------+
| BRA1 | QI | QI |
+--------+-----------+--------+
| BRA2 | Validated | QI |
+-----------------------------+
我想在等于 'QI'
时用红色突出显示单元格,在等于 'Validated'
时用绿色突出显示单元格。
这就是我所做的,但它不起作用。任何人都可以帮助修复我的脚本吗?
我的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);
?>
试试这个代码:
<?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>';
}
}
您需要样式属性并更改背景颜色
<td style="background-color: #ffffff">...</td>
我有一个 table 结构如下:
+--------+---------+----------+
|Col1 | Col2 | Col3 |
+-----------------------------+
| BRA1 | QI | QI |
+--------+-----------+--------+
| BRA2 | Validated | QI |
+-----------------------------+
我想在等于 'QI'
时用红色突出显示单元格,在等于 'Validated'
时用绿色突出显示单元格。
这就是我所做的,但它不起作用。任何人都可以帮助修复我的脚本吗?
我的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);
?>
试试这个代码:
<?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>';
}
}
您需要样式属性并更改背景颜色
<td style="background-color: #ffffff">...</td>