简体   繁体   中英

How do i highlight the option value in select box using php

I have record set where iam looping through and displaying options in select box if the record contains a value as status as "Y" than i have to display the option in red color

echo "<select name='select1' size='10' multiple >";
                        foreach ($this->arr['record'] as $rows) {
                            if($rows['status'] == "Y"){
                                echo "<option value='". $rows['COLA'] . '*' . $rows['COLB'] . "' bgcolor='#ff0000' style = 'bgcolor=red;font-size=5'>".$rows['COLC'].":".$rows['COLD'].":".$rows['COLE'].":".$rows['COLF'].":".$rows['COLG']."&nbsp;</option>";
                            }else{
                                echo "<option value='". $rows['COLA'] . '*' . $rows['COLB'] . "' bgcolor='#ff0000' style = 'bgcolor=red;font-size=5'>".$rows['COLC'].":".$rows['COLD'].":".$rows['COLE'].":".$rows['COLF'].":".$rows['COLG']."&nbsp;</option>";
                            }

                        }
echo "</select>";

Here if we have status as "Y" than we are want to highlight the entire option. I used the font tag but it is not working can you please tell us what to do

This has to do with HTML, not PHP. Set the selected attribute for the <option> .

In your case:

if($rows['status'] == "Y") {
  echo '<option selected="selected" ....
}

please just look on the generated html source code. Your question is really not related to php. Use the source view of your browser or use firebug to determine why your html attributes / css style is not applied.

Derby, you're using the style attribute the wrong way. Inside the style attribute you may specify "inline" CSS, just according to the general CSS rules.

So to highlight the selected item in red use the following code according to your needs:

echo "<option value='". $rows['COLA'] . '*' . $rows['COLB'] . "' style='background-color: red; font-size: 5pt'>".$rows['COLC'].":".$rows['COLD'].":".$rows['COLE'].":".$rows['COLF'].":".$rows['COLG']."&nbsp;</option>";

But please note, as Jason already stated, that drop-down boxes are normally handled by the Operating System, so this style MAY apply but there's no official way to style the drop-down elements.

More Info on how to use inline style sheets may be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM