Hi all,
I am implementing a dropdown list that finds people from a "State" and "City". The states and cities are stored in the DB when a user registers. Since there are several times "California" exists in the DB, how can I display "California" in my dropdown list once and then have all cities show from the same DB.
My table is like this:
id state city
1 California Los Angeles
2 California San Fransisco
3 California San Fransisco
4 California San Fransisco
5 California San Diego
6 California San Diego
7 California San Diego
8 California San Diego
9 California San Diego
..
My SQL looks like this right now...
if ($data=='states') { // first dropdown
echo "<select name='state' onChange=\"dochange('cities', this.value)\">\n";
echo "<option value='0'>==== choose state ====</option>\n";
$result=mysql_db_query($dbname,"SELECT id, state FROM comments ORDER BY state");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
} else if ($data=='cities') { // second dropdown
echo "<select name='cities' >\n";
echo "<option value='0'>====choose cities ====</option>\n";
$result=mysql_db_query($dbname,"SELECT `id`, `city` FROM comments WHERE `id` = '$val' ORDER BY `city` ");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
}
echo "</select>\n";
It seems a GROUP BY clause will only display one city from the state. Perhaps I am doing something wrong here. I am not sure how to write the SQL to perform this action.
Any help is totally appreciated...