我有一个CakePHP Web应用程序,页面顶部具有以下代码:
<section class="content">
<style>.center {text-align: center;}</style>
<!-- Small boxes (Stat box) -->
<div class="row normaluserdash">
<?php
$colors = array('', 'green','aqua','purple','orange');
foreach( $user->accounts as $account ) {
?>
<div class="normaluserdash col-xl-3 col-md-4 col-sm-6 col-12">
<!-- Card -->
<div class="card text-white bg-info">
<!-- Card Header -->
<h4 class="card-header text-white"><?=$account->title?></h4>
<!-- /card header -->
<!-- Card Body -->
<div class="card-body">
<!-- Card Title-->
<h2 class="card-title text-white center"><?=$this->Format->currency($account->balance)?></h2>
<!-- Card Title-->
</div>
<!-- /card body -->
<!-- Card Footer -->
<!-- /card footer -->
</div>
<!-- /card -->
</div>
<?php } ?>
</div>
上面的代码为用户显示4个帐户余额的帐户余额,因为上面的代码重复了4次。现在,上述所有4个块的颜色均为蓝色。我想做一些事情,让我为每个颜色提供不同的颜色,并将其输入CSS类。然后,我可以制作一个CSS样式表,该样式具有所有4种颜色的颜色。我希望所有4个块的颜色都不同,但是我希望它们在刷新后保持不变。在没有人建议的情况下,使用JS进行随机生成是行不通的。
谢谢您提供的任何帮助。如果您想知道,该网站为https://bank.northwatchbank.com,您可以注册以查看它。长话短说,这是一个假冒骗子的银行。
这里是一个主意。您应该计算颜色并根据按键获得提示,并通过提示可以从$ colors中获取颜色。如果帐户余额多于您的颜色,该功能将重复该序列。
function getCalorBykey($key, $colors){
$colors_count = count($colors);
// get reminder
$reminder = ($key)%$colors_count;
return $colors[$reminder];
}
在您的模板上,您可以像下面这样使用它:
<div class="row normaluserdash">
<?php
function getCalorBykey($key, $colors){
$colors_count = count($colors);
// get reminder
$reminder = ($key)%$colors_count;
return $colors[$reminder];
}
$colors = array('default', 'green','aqua','purple','orange');
foreach( $user->accounts as $key => $account ) {
?>
<div class="normaluserdash col-xl-3 col-md-4 col-sm-6 col-12">
<!-- Card -->
<div class="card text-white bg-info card-<?= getCalorBykey($key, $colors) ?>">
...