php - 我想使用 PHP 更改卡片 div 的背景
问题描述
我使用 for 循环从数据库中动态获取数据并显示,现在我必须使用 Php 以不同颜色显示这些卡片不需要 javascript
<div class="row gutter-xs">
<?php
foreach($dispcnt as $object)
{
?><div class="col-xs-3 col-md-3">
<div class="card bg-info"> // card
<div class="card-values">
<div class="p-x">
<small><?php echo $object->qa;?> </small>
<h3 class="card-title-l"><?php echo $object->cnt;?> Counts</h3>
</div>
</div>
</div>
</div> // code of card ends here
<?php }?>
</div>
解决方案
这是一个仅限 PHP 的解决方案。只需使用所需颜色设置一个数组,然后在遍历卡片时使用模数循环遍历它们。style
在卡片上放置一个属性来设置background
颜色。
<div class="row gutter-XS">
<?php
$colors = [
'#0298cf',
'#9b479f',
'#4e484e',
'#f70d1a',
];
$count = count($colors);
$i = 0; // increment this counter at the end of the foreach loop
foreach ($dispcnt as $object)
{
?><div class="col-xs-3 col-md-3">
<div class="card BG-info" style="background:<?php echo $colors[($i % $count)]; ?>"> <!-- card -->
<div class="card-values">
<div class="p-x">
<small><?php echo $object->QA;?> </small>
<h3 class="card-title-l"><?php echo $object->CNT;?> Counts</h3>
</div>
</div>
</div>
</div> <!-- code of card ends here -->
<?php $i++;}?>
</div>
推荐阅读
- javascript - 使用 Webpack 运行我的 React 应用程序的问题
- google-maps - 获取多边形内的折线
- python - 如何从 Windows 命令提示符中杀死 python?
- vba - 在 MS Access 应用程序中使用 VBA 填充 SQL 字符串
- c# - 在c#中将double转换为字符串
- gradle - 如何更正应用程序的类路径以使其包含单个兼容版本
- kubernetes - Kubernetes服务的外接IP地址是什么?
- ruby-on-rails - 使用第二个数据库进行 Rails 动态迁移
- javascript - 如何在用户以最佳性能滚动时检测视口中的当前元素?
- python - GRU 模型过拟合