recover data in a list for editing

Asked

Viewed 74 times

0

On an edit page of the registration data, how to fill a select with the information originally saved.

Ex. In the registration form, it was selected in a select, a city. After saving, there is option to edit the data, however I am not able to display in select the original city, and list the others, for change option.

I’m trying with php this operation, but I’m not getting it. How can I be doing?

  • 1

    See this topic: http://answall.com/questions/99107/listar-estados-cidades-bairros-em-formul%C3%a1rio-de-cadastro

1 answer

1


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.

  • Perfect Alisson, it worked right. what was missing was the $select part... now it’s working perfectly. Thank you very much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.