I think the easiest way is this:
Save all cities / states in a separate table in the database. Let's suppose that this table is formed by the columns:
id | City | UF
Then I would do the following:
<?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 cities saved in array
and not in 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 a select
to edit data.