php - 如何为视图中的非唯一记录设置相同的颜色
问题描述
我正在尝试创建表行中的相同数据是否在视图中显示为红色。
<tbody>
<?php
$i = 1;
foreach ($data as $item) :
?>
<tr>
<td>
<?php
$text = $item->serial;
if ($text == $text) {
echo '<span class="text-danger">' . $text . '</span>';
} else {
echo '<span class="text-success">' . $text . '</span>';
}
?>
</td>
</tr>
<?php
endforeach;
?>
</tbody>
解决方案
您将需要通过循环和设置serial
为键来保持序列号的计数。如果 key 再次出现,增加它。循环显示数据时,如果serial
count 大于1
,则进入red
,否则为黑色。
片段:
<tbody>
<?php
$i = 1;
$serial_count = [];
foreach($data as $item):
$serial_count[$item->serial] = (isset($serial_count[$item->serial]) ? $serial_count[$item->serial] : 0) + 1;
endforeach;
foreach ($data as $item) :
?>
<tr>
<td>
<?php
$text = $item->serial;
if ($serial_count[$text] > 1) {
echo '<span class="text-danger">' . $text . '</span>';
} else {
echo '<span class="text-success">' . $text . '</span>';
}
?>
</td>
</tr>
<?php
endforeach;
?>
</tbody>
推荐阅读
- python - 在 Selenium 中使用 driver.get 时如何防止或管理下载
- dropbox-api - Dropbox Paper API - 发布降价时视频不显示
- hadoop - 需要从应用程序 id 捕获配置单元查询
- r - 传递省略号参数以在 Reduce 内的函数调用中进行评估
- android - Android 和 REST 服务调用 - 代码审查
- mysql - 从两个表中选择并更新第三个表
- python - 子进程在完成之前终止 PowerShell 脚本
- key-value-store - 我可以使用 clickhouse 作为键值存储吗?
- javascript - 以 ajax 模式捕获事件
- c# - 清理 ASP.Net Core 2.2 中的参数