Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

php dropdown from mysql city state group

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Johnny

ASKER

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
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
Avatar of Johnny

ASKER

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