我有一个看起来像这样的表:
ID | photo | ident | status
------------------------------------------------
80 | img/photo1 | ACH3882 | V
81 | img/photo2 | SHD8837 | V
82 | img/photo3 | SFF4837 | X
83 | img/photo4 | DLL3266 | V
是否可以根据值更改行的背景色?那么,如果状态单元格的值为V,则将其设置为黄色,如果X将其变为绿色?
这是我的桌子,也是我尝试过的:
<table class="blueTable" border="2" style= "background-color: #f9f9f9; color: #000000; margin: 0 auto;" >
<thead style= "background-color: #FFFFFF">
<tr>
<th>Photo</th>
<th>Ident</th>
<th>Status</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
if ($result = $link->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
if($row['status'] == 'V') {
$style = 'style="background-color:#00FF00;';
}
if($row['status'] == 'X') {
$style = 'style="background-color:#FF00FF;';
}
echo
"<tr>
<td>{$row['photo']}</td>
<td>{$row['ident']}</td>
<td>{$row['status']}</td>
<td><a href='delete.php?id={$row['id']};'>Delete</a></td>
</tr>";
}
/*freeresultset*/
$result->free();
}
?>
</tbody>
</table>
但是背景颜色不会改变。有什么建议吗?
答案 0 :(得分:2)
您可以将status变量用作类,并在CSS中为此类添加颜色
.color-v {
background-color: blue;
}
.color-x {
background-color: green;
}
<table class="blueTable" border="2" style= "background-color: #f9f9f9; color: #000000; margin: 0 auto;" >
<thead style= "background-color: #FFFFFF">
<tr>
<th>Photo</th>
<th>Ident</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
if ($result = $link->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
echo
"<tr class='color-{$row['status']}'>
<td>{$row['photo']}</td>
<td>{$row['ident']}</td>
<td>{$row['status']}</td>
</tr>";
}
/*freeresultset*/
$result->free();
}
?>
</tbody>
</table>