Solved

php dropdown from mysql city state group

Posted on 2013-10-23
5
1,030 Views
Last Modified: 2013-10-24
im trying to set the group option in a select box to a state and list all the cities in that group of states, and its not working as expected

current code
include ("../mysqli_ctx.php");
$path="../";
$result1 = mysqli_query($mysqli,"SELECT id, state,city FROM current_loc ORDER BY state");

//echo "<pre>";print_r($result); echo "<pre>";
while($row1 = $result1->fetch_row()) {
  $rows1[]=$row1;
}

echo "<pre>";print_r($rows1); echo "</pre>";


$result = mysqli_query($mysqli,"SELECT id, city, state FROM current_loc ORDER BY state");
echo '<select style="width:300px" id="source">';
$grp_flag= TRUE;
$last_state="";
while($row = mysqli_fetch_array($result))
{
  // show here
        if ($grp_flag == TRUE) {
            echo '<optgroup label="'.$row['state'].'">'."\n";
            $grp_flag=FALSE;
        }
 
  foreach ($row as $city => $state) {
          if ($state == $row['state']) { 
          echo '<option value="' .$row['id'] . '">' . $row['city'] . '</option>'."\n";
          }
          else {
            echo "</optgroup>"."\n";
            echo '<optgroup label="'.$row['state'].'">'."\n";
          }
     }
}
echo ' </select>';


/**
 *
 * @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=" "="">Champaign</optgroup>
    </optgroup>
    <optgroup label="MA">
        <option value="">Boston</option>
    </optgroup>
    <optgroup label="OH">
        <option value="">Springfield</option>
    </optgroup>
</select>

Open in new window


current output
<select style="width:300px" id="source"><optgroup label="IL">
</optgroup>
<optgroup label="IL">
</optgroup>
<optgroup label="IL">
</optgroup>
<optgroup label="IL">
</optgroup>
<optgroup label="IL">
<option value="2">Chicago</option>
<option value="2">Chicago</option>
</optgroup>
<optgroup label="IL">
</optgroup>
<optgroup label="IL">
</optgroup>
<optgroup label="IL">
</optgroup>
<optgroup label="IL">
<option value="5">Champaign</option>
<option value="5">Champaign</option>
</optgroup>
<optgroup label="MA">
</optgroup>
<optgroup label="MA">
</optgroup>
<optgroup label="MA">
</optgroup>
<optgroup label="MA">
<option value="1">Boston</option>
<option value="1">Boston</option>
</optgroup>
<optgroup label="OH">
</optgroup>
<optgroup label="OH">
</optgroup>
<optgroup label="OH">
</optgroup>
<optgroup label="OH">
<option value="3">Columbus</option>
<option value="3">Columbus</option>
</optgroup>
<optgroup label="OH">
</optgroup>
<optgroup label="OH">
</optgroup>
<optgroup label="OH">
</optgroup>
<optgroup label="OH">
<option value="4">Springfield</option>
<option value="4">Springfield</option>
 </optgroup></select>

Open in new window


should be similar to this
<select style="width:300px" id="source">
    <optgroup label="IL">
        <option value"="0">Champaign</option>
        <option value"="5">Chicago</option>
   </optgroup>
    
    <optgroup label="MA">
        <option value="1">Boston</option>
    </optgroup>
    <optgroup label="OH">
        <option value="2">Springfield</option>
    </optgroup>
</select>

Open in new window


and ideas where my code is wrong please
thank you In Advance for any code or help you may provide
0
Comment
Question by:Johnny
  • 3
  • 2
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39597163
I'll take a look at this, but while I'm doing that, you might want to read this article about how to use MySQLi, including query error handling.  I think you would find the OO version of MySQLi to have easier syntax.  And fetch_array() is an AntiPattern because of poor performance.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39597243
Obviously this is untested code, but it should be correct in principle.  The select control is built in a variable called $out, which can be included in the HTML output template or echo'ed into the browser output stream at the appropriate moment.

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
        $out .= '<optgroup label="' . $row->state . '">' . 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;

Open in new window

HTH, ~Ray
0
 

Author Closing Comment

by:Johnny
ID: 39597944
Thank you ray as always i love your comments and code examples
now to figure out how to add images to the optgroup for state flags
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39597969
Images for state flags... Interesting!  Hope you'll post a question about that here at EE.

Thanks for the points and best of luck with the project, ~Ray
0
 

Author Comment

by:Johnny
ID: 39598100
heres the question for icon before select optgroup in a pulldown
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28276353.html
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now