我正在将数据库中的数据回显到 html 表中,有没有办法让每隔一行的背景颜色都不同,类似于平衡支票簿的颜色模式?
if ($serviceResults = $db->query("SELECT * FROM services WHERE technician_id = $userid ")){
if($serviceResults->num_rows){
while($row = $serviceResults->fetch_object()){
$records[] = $row;
}
$serviceResults->free();
}
}
if(!count($records)){
echo "Please Add a Service";
} else {
?>
<table>
<thead>
<tr>
<th>Service Category</th>
<th>Service Name</th>
<th>Service Time</th>
<th>Service Cost</th>
</tr>
</thead>
<tbody>
<?php
foreach($records as $r){
?>
<tr>
<td><?php echo escape($r->serviceCategory); ?></td>
<td><?php echo strtoupper(escape($r->serviceName)); ?></td>
<td><?php echo gmdate("H:i" , $r->serviceTime) . ' <b>Hr/Min</b>'; ?></td>
<td class="cost"><?php echo '$' . escape($r->serviceCost); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
目前我的数据库中有 10 个计数,这与表格很好地呼应,但我希望每个偶数行都显示灰色,奇数行显示白色。这可能吗?
最佳答案
您可以将以下 css 添加到 <style></style>
页面中的元素。
tr:nth-child(even) {background: #666666}
tr:nth-child(odd) {background: #FFF}
参见 this获取更多信息。
希望对你有帮助
关于php - 如何在php中回显不同的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26942662/