0

So i have this code that creates a SELECT in a FORM, i was wondering how can i make it to add a different background color when $value is different.

Please have in mind that $value is not always the same text and may change.

PHP CODE:

<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>';

RESULT:

 <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>

WANTED RESULT:

<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 Answers 4

1

Try this solution

// 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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

ok ur code adds the background color...the problem is $step = 3; it telling the code to change color after 3 lines...when it should change the color for the values that are the same... if $value equals Lower Deck than print RED...just have in mind the text could change
So you need, to have a comparison table where you say Lower Deck == 'red' , Main Deck == 'white' and so on, give me a sec I will write another answer based on this
yes thats what im looking for...the only problem like i told u is that $value is not always the same...Lower Deck could change to other text like FRONT DECK...the code should change the color only if the $value is not the same
thanks, your other post is really close...only thing to fix is that values could change in $decks_color
hey sorry for the dely, ur code worked! quick question it starts with white color instead of RED...
|
1

Try this one

// 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>';
}

1 Comment

now thats what im taking about...only problem is that text changes
0

I have not tested this, but here is the gist...

You need to create an array of colors, then in your loop, check to see if the value has changed. If it has, increase the index, set a the color variable, and keep moving...

<?
$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;

2 Comments

yes, if you want more colors, add them to the colors array.
$colors = array('red', 'blue', 'green', 'yellow', '#789899', 'rgba(200, 54, 54, 0.5)', '#023');
0
$f.= "<option style=background-color:".[YOUR VALUE VARIABLE]."
value='".$value."'>".$value
/$type_array[$key]."</option>";

Personally enclosing all non-php with " " instead of ' ' is easier to maintain. Also using ' ' in side of non-php for the HTML to process.

Your code does not produce any value for "background-color". It seems you forgot a variable after "background-color" and instead moved on to the value property.

4 Comments

does not answer mi inquiry
<option style="background-color:" value="'.$value.'"> is <option style=background-color:; value="RED"> instead you need to define a variable for the background-color itself. <option style="background-color:RED" value="'.$value.'">
i did not forget the background-color value...its just an example of how the output is at the moment...
<option style='"background-color:".$color."'> Where is the $color variable in your code? I am trying to help you out. But you are not giving clear responses as to what you actually need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.