1

I have a php loop where I am trying to echo a HTML image source inside the select option tag, like this:

foreach($recs as $i){
    if($i->isscripted == 1){
      $is = "<img src='greencheck.gif'>"; 
    }else{
      $is = "<img src='redbang.gif'>"; 
    }

   echo "<option value=".$i->id.">".$is."  ".$i->fullname."</option>";
}

The image is not displaying in the select list. I tested the image tag outside of the loop and it displays fine.

echo "<img src='greencheck.gif'>";
jamesTheProgrammer
  • 1,747
  • 4
  • 22
  • 34

4 Answers4

5

You cannot do it using pure HTML, but there are a few workarounds:

  1. CSS Background (Works only in firefox):

    <select>
        <option value="volvo" style="background-image:url(images/volvo.png);">Volvo</option>
        <option value="saab"  style="background-image:url(images/saab.png);">Saab</option>
        <option value="honda" style="background-image:url(images/honda.png);">Honda</option>
        <option value="audi"  style="background-image:url(images/audi.png);">Audi</option>
    </select>
    
  2. Using a jQuery Plugin

    JavaScript Image Dropdown

    Are you tired with your old fashion dropdown? Try this new one. Image combo box. You can add an icon with each option. It works with your existing "select" element or you can create by JSON object.

    http://www.queness.com/resources/images/formplugin/22.gif

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
1

A nice solution from vartec

You can try it and thanks to him

Click Here

Community
  • 1
  • 1
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
1

You can use:

foreach($recs as $i){
    if($i->isscripted == 1){
      $is = "greencheck.gif"; 
    }else{
      $is = "redbang.gif"; 
    }

   echo "<option value=".$i->id."  style='background-image:url(".$is."); background-repeat:no-repeat; padding-left:30px;'>".$i->fullname."</option>";
}
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
0

You cant have images inside <option>,it can take only textual options, you will need to get it out and maybe do some javascript or jquery to display the image somewhere else when you select an option. There are also Jquery pluggins that can do this as http://designwithpc.com/Plugins/ddSlick

pts
  • 80,836
  • 20
  • 110
  • 183
Luis Tellez
  • 2,785
  • 1
  • 20
  • 28