Solved

Display image icon before select optgroup in pulldown

Posted on 2013-10-24
10
1,333 Views
Last Modified: 2013-10-30
i came across this jquery plugin and thought it be nice to have a grouped states with cities under it with the states having icons

thanks to the fantastic ray, i was able to have a pulldown with grouped citys by states
see code here php pulldown - grouped

see templates example of sorta what id like to do here icons in pulldowns
i know its converting it to a div. to add the icons and a nifty search box too.

i did have it doing this before as plain code but when i set out to pull the info from mysql i took it out and lost how to do this. so i know its possible.

i have tried to place an image html source before and after the optgroup ive tryed background images too.

code so far
      <style type="text/css">

        img.flag { height: 15px; width: 20px; padding-right: 10px; }

      </style>
<?
include ("../mysqli_ctx.php");
$path="../";

// CREATE QUERY STRING
$sql = "SELECT id, city, state FROM current_loc ORDER BY state, city";

// RUN QUERY
$result = mysqli_query($mysqli, $sql);
/**
 * TEST FOR QUERY SUCCESS HERE, POSSIBLY TRIGGER_ERROR()
 */

// START THE SELECT CONTROL
$out = '<select style="width:300px" id="source">' . PHP_EOL;

// THE CURRENT STATE IS KEPT HERE
$old = '?';

// THE INITIAL OPTGROUP STATUS IS KEPT HERE
$grp = FALSE;

// USE AN ITERATOR TO RETRIEVE THE ROWS OF THE RESULTS SET
while($row = mysqli_fetch_object($result))
{
    // IF THIS IS A ROW WITH A NEW STATE?
    if ($row->state != $old)
    {
        // IF THERE IS AN EXISTING OPT-GROUP
        if ($grp)
        {
            // TIE OFF THE OLD OPT-GROUP
            $out .= '</optgroup>' . PHP_EOL;
        }
        
        // ADD THE OPT-GROUP FOR THE NEW STATE
        $flag = '<img class="flag" src="'.$path.'images/flags/'.strtolower($row->state).'.png">';
        $out .= $flag.'<optgroup label="' . $row->state . '" style="background-image:url(../images/flags/'.strtolower($row->state).'.png)">' . PHP_EOL;
        $out .= '<option value="' . $row->id . '">' . $row->city . '</option>' . PHP_EOL;
        
        // CHANGE STATES
        $old = $row->state;
        $grp = TRUE;
    }
    // IF THIS IS ANOTHER CITY IN THE SAME STATE
    else
    {
        // JUST ADD THE CITY OPTION
        $out .= '<option value="' . $row->id . '">' . $row->city . '</option>' . PHP_EOL;
    }
}
// AT THE END OF THE RESULTS SET, TIE OFF THE OPT-GROUP AND THE SELECT CONTROL
$out .= '</optgroup>' . PHP_EOL;
$out .= '</select>' . PHP_EOL;

echo $out;
/**
 *
 * @todo       do
 * @date       The current date is: Wed 10/23/2013 
Enter the new date: (mm-dd-yy)  *
 */


?>
<select style="width:300px" id="source">
    <optgroup label="IL">
        <option value="" style="background-image:url(../images/flags/il.png);">Champaign</optgroup>
    </optgroup>
    <img class="flag" src="../images/flags/ma.png"><optgroup label="MA">
        <option value="">Boston</option>
    </optgroup>
    <optgroup label="OH"><img class="flag" src="../images/flags/oh.png">
        <option value="">Springfield</option>
    </optgroup>
</select>

<img class="flag" src="../images/flags/il.png">

Open in new window


im open to doing this via jquery and adding a search box too like the one in select 2.4.3(template). or how ever we can get it to function.

thank in advance for any code or help you may provide.
Johnny
0
Comment
Question by:Johnny
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39600073
Can you please show us the HTML that is generated by that PHP script?  If you have it on a server, just send us the link to the SSCCE with a few rows of state/city data, thanks.
0
 

Author Comment

by:Johnny
ID: 39600244
the above codes output
<style type="text/css">

        img.flag { height: 15px; width: 20px; padding-right: 10px; }

      </style>
<select style="width:300px" id="source">
<img class="flag" src="../images/flags/il.png"><optgroup label="IL" style="background-image:url(../images/flags/il.png)">
<option value="5">Champaign</option>
<option value="2">Chicago</option>
</optgroup>
<img class="flag" src="../images/flags/ma.png"><optgroup label="MA" style="background-image:url(../images/flags/ma.png)">
<option value="1">Boston</option>
</optgroup>
<img class="flag" src="../images/flags/oh.png"><optgroup label="OH" style="background-image:url(../images/flags/oh.png)">
<option value="3">Columbus</option>
<option value="4">Springfield</option>
</optgroup>
</select>
<select style="width:300px" id="source">
    <optgroup label="IL">
        <option value="" style="background-image:url(../images/flags/il.png);">Champaign</optgroup>
    </optgroup>
    <img class="flag" src="../images/flags/ma.png"><optgroup label="MA">
        <option value="">Boston</option>
    </optgroup>
    <optgroup label="OH"><img class="flag" src="../images/flags/oh.png">
        <option value="">Springfield</option>
    </optgroup>
</select>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39600601
But what is the URL?  I think we need to follow the file paths.
0
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39600692
I'll tinker with this a bit.  Not sure if it matters, but the style declarations usually end with a semi-colon.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39600792
Some of the style attributes seem to apply and others are ignored.
http://www.laprbass.com/RAY_temp_pern.php

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
img.flag {
    height: 15px;
    width: 20px;
}
.myOptGroup {
    color:red;
    margin: 0px 0px 0px 44px;
    background-image:url('http://completelocal.info/images/flags/oh.png');
    background-color:blue;
}
option {
    color:green;
}
select {
    width:300px;
}
</style>

</head>
<body>
<!-- http://completelocal.info/images/flags/oh.png -->
<select>
  <optgroup label="Swedish Cars" class="myOptGroup">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars" style="margin: 0px 0px 0px 144px; background-image:url('http://completelocal.info/images/flags/il.png');">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
</select>

</body>
</html>

Open in new window

0
 

Author Comment

by:Johnny
ID: 39601042
Yeah its odd isn't it
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39601069
Looking at this, it seems you can add the images to the options, but I didn't see any added to the optgroups.  It may not have browser support (even if it should).
http://ivaynberg.github.io/select2/
0
 

Author Closing Comment

by:Johnny
ID: 39610816
abandoned this as it does not seam to be possible, thx for the help
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39611763
Sorry there was no magic bullet, but thanks for the points anyway.  Best, ~Ray
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question