Php改变不同值的背景颜色

时间:2014-03-10 16:30:20

标签: php loops foreach

所以我有这个代码在FORM中创建一个SELECT,我想知道如何在$ value不同时添加不同的背景颜色。

请注意,$value并非总是相同的文字,可能会发生变化。

PHP代码:

<div class="line"><select style="width: 100%;" name="fields['.base64_encode('Itinerary - Days').']" class="required">
  <option value="">--- Select Deck & Itinerary ---</option>';

foreach($main_title_array as $key => $value):    
$f.= '<option style="background-color:" value="'.$value.'">'.$value.' / '.$type_array[$key].'</option>';
endforeach;

$f.='</select></div>';

结果:

 <select class="required" name="fields[SXRpbmVyYXJ5IC0gRGF5cw==]" style="width: 100%;">
      <option value="">--- Select Deck &amp; Itinerary ---</option>
      <option value="Lower Deck" style="background-color:">Lower Deck / Discovery Itin 4D &ndash; 3N</option>
      <option value="Lower Deck" style="background-color:">Lower Deck / Discovery Itin 5D &ndash; 4N</option>
      <option value="Lower Deck" style="background-color:">Lower Deck / Discovery Itin 8D &ndash; 7N</option>
      <option value="Main Deck" style="background-color:">Main Deck / Discovery Itin 4D &ndash; 3N</option>
      <option value="Main Deck" style="background-color:">Main Deck / Discovery Itin 5D &ndash; 4N</option>
      <option value="Main Deck" style="background-color:">Main Deck / Discovery Itin 8D &ndash; 7N</option>
      <option value="Upper Deck" style="background-color:">Upper Deck / Discovery Itin 4D &ndash; 3N</option>
      <option value="Upper Deck" style="background-color:">Upper Deck / Discovery Itin 5D &ndash; 4N</option>
      <option value="Upper Deck" style="background-color:">Upper Deck / Discovery Itin 8D &ndash; 7N</option>
    </select>

通缉结果:

<select class="required" name="fields[SXRpbmVyYXJ5IC0gRGF5cw==]" style="width: 100%;">
  <option value="">--- Select Deck &amp; Itinerary ---</option>
  <option value="Lower Deck" style="background-color:red">Lower Deck / Discovery Itin 4D &ndash; 3N</option>
  <option value="Lower Deck" style="background-color:red"">Lower Deck / Discovery Itin 5D &ndash; 4N</option>
  <option value="Lower Deck" style="background-color:red"">Lower Deck / Discovery Itin 8D &ndash; 7N</option>
  <option value="Main Deck" style="background-color:white">Main Deck / Discovery Itin 4D &ndash; 3N</option>
  <option value="Main Deck" style="background-color:white">Main Deck / Discovery Itin 5D &ndash; 4N</option>
  <option value="Main Deck" style="background-color:white">Main Deck / Discovery Itin 8D &ndash; 7N</option>
  <option value="Upper Deck" style="background-color:black">Upper Deck / Discovery Itin 4D &ndash; 3N</option>
  <option value="Upper Deck" style="background-color:black">Upper Deck / Discovery Itin 5D &ndash; 4N</option>
  <option value="Upper Deck" style="background-color:black">Upper Deck / Discovery Itin 8D &ndash; 7N</option>
</select>

4 个答案:

答案 0 :(得分:1)

试试这个解决方案

// List of colors
$colors = array('red', 'white', 'black','blue');

//The lenght of the colors available
$lenght_colors = count($colors);

//store last deck
$last_deck = null;

//Counters
$count = 0;

// Avoid warnings
$f = null;

foreach($main_title_array as $key => $value)
{
    // Switch colors
    if($last_deck != $value)
    {
        // Check if you reached the limit of colors available if yes reset the count
        if($count != $lenght_colors)
        {
            $count++;
        }
        else
        {
            $count = 0;
        }
    }

    $f .= '<option style="background-color:'.$colors[$count].'" value="'.$value.'">'.$value.' / '.$type_array[$key].'</option>';
    $last_deck = $value;
}

答案 1 :(得分:1)

试试这个

// Decks Color
$decks_color = array('Lower Deck' => 'red', 'Main Deck' => 'white', 'Upper Deck' => 'black');

// Avoid warnings
$f = null;

foreach($main_title_array as $key => $value)
{
    $f .= '<option style="background-color:'.$decks_color[trim($value)].'" value="'.$value.'">'.$value.' / '.$type_array[$key].'</option>';
}

答案 2 :(得分:0)

我没有对此进行过测试,但这里是要点...

您需要创建一个颜色数组,然后在循环中检查值是否已更改。如果有,增加索引,设置颜色变量,并继续移动......

<?
$valCheck = "";
$colors = array('red', 'white', '#023');
$count = 0;
$color = $colors[$count];

foreach($main_title_array as $key => $value):    

    if($valCheck != $value){
    $color = $colors[$count];
    $valCheck = $value;
    $count++;
    }
    $f.= '<option style="background-color:'.$color.'" value="'.$value.'">'.$value.' / '.$type_array[$key].'</option>';

endforeach;

答案 3 :(得分:0)

$f.= "<option style=background-color:".[YOUR VALUE VARIABLE]."
value='".$value."'>".$value
/$type_array[$key]."</option>";

个人用“”代替''包含所有非PHP的内容更容易维护。还要在非PHP的一边使用''来处理HTML。

您的代码不会为“background-color”生成任何值。您似乎忘记了“background-color”之后的变量,而是转移到value属性。