打印背景与两种不同的颜色

时间:2014-08-15 22:27:39

标签: php colors

我需要用两种不同的颜色打印“背景”。例如

1º <span background="#ccc"></span>
2º <span background="#000"></span>
3º <span background="#ccc"></span>
4º <span background="#000"></span>
5º <span background="#ccc"></span>
6º <span background="#000"></span>
...

我正在使用“Foreach”。我该如何打印?

我的代码是:

  foreach ($inv as $invoi)
   {

    $valuer = 0;
    $valuer = 0 + $invoi["SOMA"];

       $str = str_replace('.', '', $valuer);


    $vale = number_format($valuer, 2, '.', '.') . "";

$plus = 5000000;
$minus = 0;


$totalnumber = $plus + $minus;

$pluspercent = round(($str / $plus) * 100);

$total = round($plus + $minus);
$totalVotes = $total;

$findmax = max($str); 
$findmin = min($str); 


$tbody .=  '<li><span style="background: #DifferentColor; bottom: 0; text-align: center; height:'.$pluspercent.'%">'.$vale.'</span></li>';



   }

我需要制作一个图形,我会为每个条形添加不同的颜色,例如:black |白色|黑色|白

也许有人可以帮助我?

谢谢:)

1 个答案:

答案 0 :(得分:0)

在for循环之前设置变量以保存颜色的值:

$currentcolor = '#ccc' 

//the for loop begin here

显示当前颜色:

$tbody .=  '<li><span style="background: ' . $currentcolor . '; bottom: 0; text-align: center; height:'.$pluspercent.'%">'.$vale.'</span></li>';

最后,更改颜色的值(在for循环结束时):

$currentcolor = $currentcolor == '#ccc' ? '#000' : '#ccc';

修改 这是完整的代码:

$currentcolor = '#00db10'; 
$invlenght = count($inv);
$count = 0;

foreach ($inv as $invoi){

    //test if it is last iteration here
    if($count == $invlenght - 1){ $currentcolor = "#ff0000"; } 

    $valuer = $invoi["SOMA"];
    $date = $invoi["date"];

   $str = str_replace('.', '', $valuer);
   $vale = number_format($valuer, 2, '.', '.') . "";
   $plus = 4000000;
   $minus = 0;
   $totalnumber = $plus + $minus;

   $pluspercent = round(($str / $plus) * 100);

   $total = round($plus + $minus);
   $totalVotes = $total;

   $findmax = max($str); 
   $findmin = min($str); 

   $tbody .=  '<li><span style="background: ' . $currentcolor . '; bottom: 0; text-align:         center; height:'.$pluspercent.'%"><span class="showmonth">'.$showdate.'</span><span class="fixdivg">€'.$vale.' EUR</span></span></li>';


   $currentcolor = $currentcolor == '#00db10' ? '#00b90e' : '#00db10';
   $count++;
}