Solved

php dropdown from mysql city state group

Posted on 2013-10-23
5
1,104 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
[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
  • 3
  • 2
5 Comments
 
LVL 110

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 110

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 110

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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