I think the easiest way is this:
Save all cities/states in a separate table in the database. Let’s assume that this table is formed by columns:
id | City | UF
So here’s what I’d do:
<?php
$query = mysql_query("SELECT * FROM cidades ORDER BY cidade ASC");
echo '<select name="cidade">';
while($exe = mysql_fetch_array($query)){
$selected = ($exe['id'] == $id_salva_da_cidade_na_outra_tabela) ? 'selected' : '';
echo '<option value="'.$exe['id'].'" '.$selected.'>'.$exe['cidade'].' - '.$exe['uf'].'</option>';
}
echo '</select>';
?>
If you have the cities saved in one array
and not in a database:
<?php
$array = array('sp'=>'São Paulo', 'sp'=>'Osasco', 'rj'=>'Rio de Janeiro');
echo '<select name="cidade">';
foreach($array as $uf=>$cidade){
$selected = ($uf == $uf_salva_no_banco_de_dados) ? 'selected' : '';
echo '<option value="'.$uf.'" '.$selected.'>'.$cidade.' - '.$uf.'</option>';
}
echo '</select>';
?>
I use these outputs when I have to auto-fill one select
to edit data.
See this topic: http://answall.com/questions/99107/listar-estados-cidades-bairros-em-formul%C3%a1rio-de-cadastro
– Diego