Johnny
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
current output
should be similar to this
and ideas where my code is wrong please
thank you In Advance for any code or help you may provide
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>
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>
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>
and ideas where my code is wrong please
thank you In Advance for any code or help you may provide
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
Thanks for the points and best of luck with the project, ~Ray
ASKER
heres the question for icon before select optgroup in a pulldown
http://www.experts-exchang e.com/Web_ Developmen t/Web_Lang uages-Stan dards/PHP/ Q_28276353 .html
http://www.experts-exchang
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