loops - Php change background color on different value -
so have code creates select in form, wondering how can make add different background color when $value different.
please have in mind $value
not same text , 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 $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 & itinerary ---</option> <option value="lower deck" style="background-color:">lower deck / discovery itin 4d – 3n</option> <option value="lower deck" style="background-color:">lower deck / discovery itin 5d – 4n</option> <option value="lower deck" style="background-color:">lower deck / discovery itin 8d – 7n</option> <option value="main deck" style="background-color:">main deck / discovery itin 4d – 3n</option> <option value="main deck" style="background-color:">main deck / discovery itin 5d – 4n</option> <option value="main deck" style="background-color:">main deck / discovery itin 8d – 7n</option> <option value="upper deck" style="background-color:">upper deck / discovery itin 4d – 3n</option> <option value="upper deck" style="background-color:">upper deck / discovery itin 5d – 4n</option> <option value="upper deck" style="background-color:">upper deck / discovery itin 8d – 7n</option> </select>
wanted result:
<select class="required" name="fields[sxrpbmvyyxj5ic0grgf5cw==]" style="width: 100%;"> <option value="">--- select deck & itinerary ---</option> <option value="lower deck" style="background-color:red">lower deck / discovery itin 4d – 3n</option> <option value="lower deck" style="background-color:red"">lower deck / discovery itin 5d – 4n</option> <option value="lower deck" style="background-color:red"">lower deck / discovery itin 8d – 7n</option> <option value="main deck" style="background-color:white">main deck / discovery itin 4d – 3n</option> <option value="main deck" style="background-color:white">main deck / discovery itin 5d – 4n</option> <option value="main deck" style="background-color:white">main deck / discovery itin 8d – 7n</option> <option value="upper deck" style="background-color:black">upper deck / discovery itin 4d – 3n</option> <option value="upper deck" style="background-color:black">upper deck / discovery itin 5d – 4n</option> <option value="upper deck" style="background-color:black">upper deck / discovery itin 8d – 7n</option> </select>
try solution
// list of colors $colors = array('red', 'white', 'black','blue'); //the lenght of colors available $lenght_colors = count($colors); //store last deck $last_deck = null; //counters $count = 0; // avoid warnings $f = null; foreach($main_title_array $key => $value) { // switch colors if($last_deck != $value) { // check if reached limit of colors available if yes reset 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; }
Comments
Post a Comment