Solved

Display image icon before select optgroup in pulldown

Posted on 2013-10-24
10
1,237 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
  • 6
  • 4
10 Comments
 
LVL 109

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 109

Expert Comment

by:Ray Paseur
ID: 39600601
But what is the URL?  I think we need to follow the file paths.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Johnny
ID: 39600680
0
 
LVL 109

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 109

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 109

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 109

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Php recording post 4 46
Sort Order Icons on HTML Table 3 24
Select case on click 3 16
Jquery 2 13
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

809 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