[英]PHP - Add specific background color in table row
我有一个具有以下结构的表:
+--------+---------+----------+
|Col1 | Col2 | Col3 |
+-----------------------------+
| BRA1 | QI | QI |
+--------+-----------+--------+
| BRA2 | Validated | QI |
+-----------------------------+
我想用红色表示等于'QI'
单元格,而用绿色表示等于'Validated'
。
这是我所做的,但是没有用。 任何人都可以帮助修复我的脚本吗?
<?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>
试试这个代码:
<?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.