Php 基于字段值的备用行颜色
考虑以下代码:Php 基于字段值的备用行颜色,php,Php,考虑以下代码: <?php foreach($payment_type->revisionHistory as $history): ?> <tr> <td><?= $history->userResponsible()->alias ?></td> <td><?= $history->fieldName() ?></td>
<?php foreach($payment_type->revisionHistory as $history): ?>
<tr>
<td><?= $history->userResponsible()->alias ?></td>
<td><?= $history->fieldName() ?></td>
<td><?= $history->oldValue() ?></td>
<td><?= $history->newValue() ?></td>
<td><?= $history->created_at ?></td>
</tr>
<?php endforeach; ?>
我希望只有在字段值处创建的与上一次迭代不同时,才能更改行颜色,并在值相同时保持相同的行颜色
我尝试了一些方法,但结果非常糟糕。您应该将其与css混合使用,但这非常简单:
<?php
$lastCreatedAt = ''; // for storing value of the previous date, start with empty
$classNameEven = TRUE; // begin with "even" class
foreach($payment_type->revisionHistory as $history):
$switchClass = ($lastCreatedAt != $history->created_at);
if ($switchClass) {
$classNameEven = !$classNameEven;
}
?>
<tr class="<?php echo ($classNameEven ? 'even' : 'odd'); ?>">
<td><?= $history->userResponsible()->alias ?></td>
<td><?= $history->fieldName() ?></td>
<td><?= $history->oldValue() ?></td>
<td><?= $history->newValue() ?></td>
<td><?= $history->created_at ?></td>
</tr>
<?php //remember the last date for next iteration
$lastCreatedAt = $history->created_at;
?>
<?php endforeach; ?>
@虫族三元会更好。这真的很好,谢谢。我从未见过构造$classnameen=$ClassName即使
,它是否替换布尔值?是的,它将其取反-根据初始值将其从TRUE
更改为FALSE
,反之亦然。
<style>
table tr.even td {
background-color: #FFF;
}
table tr.odd td {
background-color: #999; //odd rows have darker background
}
</style>