PF2G Posted June 4, 2012 at 09:33 AM Report Share #460191 Posted June 4, 2012 at 09:33 AM (edited) Bom dia, a todos Eu estou a ir buscar a informação de um resultado e tenho na primeira combobox a lista de identificadores que existem na BD. Mas eu queria que me pusesse selccionado o identificador respetivo ao id selecionado <?PHP include 'db_connect.php'; session_start(); if (isset($_GET['id'])) { $sel_id = "SELECT * FROM content_lang WHERE id='".$_GET['id']."'"; $ident = mysql_query($sel_id, $connect); $row_id = mysql_fetch_assoc($ident); $sel_ident="SELECT * FROM content"; $indentif=mysql_query($sel_ident, $connect); $row_ident=mysql_fetch_assoc($indentif); ?> <html> <head> <title>Administração - Editar</title> <script type="text/javascript" src="ckeditor/ckeditor.js"></script> <script src="ckeditor/_samples/sample.js" type="text/javascript"></script> <link href="ckeditor/_samples/sample.css" rel="stylesheet" type="text/css" /> </head> <body style="color:#FFFFFF;" bgcolor="#666666"> <form action="" method="post"> <table > <tr> <td align="right"> Identificador: </td> <td> <select name="identifier"> <?php do{ $sel_cont="SELECT * FROM content WHERE id_content='".$row_id['id_content']."'"; $cont=mysql_query($sel_cont, $connect); $row_cont=mysql_fetch_assoc($cont); $sel_cont_lang="SELECT * FROM content_lang WHERE id_content='".$row_cont['id_content']."'"; $cont_lang=mysql_query($sel_cont_lang, $connect); $row_cont_lang=mysql_fetch_assoc($cont_lang); ?> <option value="<?php echo $row_id['id_content']; ?>"> <?PHP echo $row_ident['identifier']; ?> </option> <?PHP } while ($row_ident=mysql_fetch_assoc($indentif)); ?> </select> </td> </tr> <tr height="10px"></tr> <tr> <td align="right"> Linguagem: </td> <td> <select name="lang"> <option value="pt"> Português </option> <option value="en"> Inglês </option> </select> </td> </tr> <tr height="10px"></tr> <tr> <td align="right"> Título: </td> <td> <input value="<?php echo $row_cont_lang['titulo']; ?>" type="text" name="titulo"> </input> </td> </tr> <tr height="10px"></tr> <tr> <td align="right" valign="top"> Conteúdo: </td> <td> <textarea class="ckeditor" name="descricao" cols="50" rows="10"><?php echo $row_id['descricao']; ?></textarea> </td> </tr> <tr> <td></td> <td> <input type="submit" name="update" value="Editar Informação" /> <a href="admin.php"><input type="button" name="update" value="Voltar"/></a> </td> </tr> </table> </form> </body> </html> <?php if (isset($_POST['update'])) { $identifier=$_POST['identifier']; $lang=$_POST['lang']; $titulo=$_POST['titulo']; $descricao=$_POST['descricao']; $update_info="UPDATE content_lang SET id_content='".$identifier."', lang='".$lang."', titulo='".$titulo."', descricao='".$descricao."' WHERE id='".$row_id['id']."'"; $info=mysql_query($update_info, $connect); if (!$info) { echo '<script>alert("Erro na edição devido a um erro no sistem. Tente mais tarde"); location.href="admin.php"<script>'; } else { echo '<script>alert("Informação editada com sucesso."); location.href="admin.php";</script>'; } } } ?> Alguém me pode ajudar? Edited June 4, 2012 at 09:33 AM by PF2G Link to comment Share on other sites More sharing options...
pmg Posted June 4, 2012 at 09:44 AM Report Share #460198 Posted June 4, 2012 at 09:44 AM (edited) Para meteres um determinado valor seleccionado numa combobox, usa o atributo "selected", por exemplo <select name="whatever"> <option value="0">Selecciona tipo <option value="1">um</option> <option value="2" selected>dois</option> <option value="3">tres</option> </select> Para fazer isto com php só precisas dum if e dum echo echo '<option ...'; if (OPCAOSELECCIONADA == OPCAOBASEDADOS) echo ' selected'; echo '>'; echo TEXTODAOPCAO; echo '</option>'; Edited June 4, 2012 at 09:45 AM by pmg What have you tried? Não respondo a dúvidas por PM A minha bola de cristal está para compor; deve ficar pronta para a semana. Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código! Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted June 4, 2012 at 09:45 AM Report Share #460199 Posted June 4, 2012 at 09:45 AM tens de verificar se o elemento do select a ser apresentado tem o valor seleccionado e adicionares o código html selected: <option ... selected="selected">...</option> IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted June 4, 2012 at 09:52 AM Author Report Share #460203 Posted June 4, 2012 at 09:52 AM (edited) Para meteres um determinado valor seleccionado numa combobox, usa o atributo "selected", por exemplo <select name="whatever"> <option value="0">Selecciona tipo <option value="1">um</option> <option value="2" selected>dois</option> <option value="3">tres</option> </select> Para fazer isto com php só precisas dum if e dum echo echo '<option ...'; if (OPCAOSELECCIONADA == OPCAOBASEDADOS) echo ' selected'; echo '>'; echo TEXTODAOPCAO; echo '</option>'; Não fica assim: <?PHP include 'db_connect.php'; session_start(); if (isset($_GET['id'])) { $sel_id = "SELECT * FROM content_lang WHERE id='".$_GET['id']."'"; $ident = mysql_query($sel_id, $connect); $row_id = mysql_fetch_assoc($ident); $sel_ident="SELECT * FROM content"; $indentif=mysql_query($sel_ident, $connect); $row_ident=mysql_fetch_assoc($indentif); ?> <html> <head> <title>Administração - Editar</title> <script type="text/javascript" src="ckeditor/ckeditor.js"></script> <script src="ckeditor/_samples/sample.js" type="text/javascript"></script> <link href="ckeditor/_samples/sample.css" rel="stylesheet" type="text/css" /> </head> <body style="color:#FFFFFF;" bgcolor="#666666"> <form action="" method="post"> <table > <tr> <td align="right"> Identificador: </td> <td> <select name="identifier"> <?php do{ $sel_cont="SELECT * FROM content WHERE id_content='".$row_id['id_content']."'"; $cont=mysql_query($sel_cont, $connect); $row_cont=mysql_fetch_assoc($cont); $sel_cont_lang="SELECT * FROM content_lang WHERE id_content='".$row_cont['id_content']."'"; $cont_lang=mysql_query($sel_cont_lang, $connect); $row_cont_lang=mysql_fetch_assoc($cont_lang); ?> <option value="<?php echo $row_id['id_content']; if ($row_ident['id_content']==$row_id['id_content']){echo ' selected="selected"';} ?>"> <?PHP echo $row_ident['identifier']; ?> </option> <?PHP } while ($row_ident=mysql_fetch_assoc($indentif)); ?> </select> </td> </tr> <tr height="10px"></tr> <tr> <td align="right"> Linguagem: </td> <td> <select name="lang"> <option value="pt"> Português </option> <option value="en"> Inglês </option> </select> </td> </tr> <tr height="10px"></tr> <tr> <td align="right"> Título: </td> <td> <input value="<?php echo $row_cont_lang['titulo']; ?>" type="text" name="titulo"> </input> </td> </tr> <tr height="10px"></tr> <tr> <td align="right" valign="top"> Conteúdo: </td> <td> <textarea class="ckeditor" name="descricao" cols="50" rows="10"><?php echo $row_id['descricao']; ?></textarea> </td> </tr> <tr> <td></td> <td> <input type="submit" name="update" value="Editar Informação" /> <a href="admin.php"><input type="button" name="update" value="Voltar"/></a> </td> </tr> </table> </form> </body> </html> <?php if (isset($_POST['update'])) { $identifier=$_POST['identifier']; $lang=$_POST['lang']; $titulo=$_POST['titulo']; $descricao=$_POST['descricao']; $update_info="UPDATE content_lang SET id_content='".$identifier."', lang='".$lang."', titulo='".$titulo."', descricao='".$descricao."' WHERE id='".$row_id['id']."'"; $info=mysql_query($update_info, $connect); if (!$info) { echo '<script>alert("Erro na edição devido a um erro no sistem. Tente mais tarde"); location.href="admin.php"<script>'; } else { echo '<script>alert("Informação editada com sucesso."); location.href="admin.php";</script>'; } } } ?> Edited June 4, 2012 at 09:56 AM by PF2G Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted June 4, 2012 at 09:54 AM Report Share #460204 Posted June 4, 2012 at 09:54 AM olha bem para o que fizeste e o código que te foi fornecido: o código html selected="selected" à um argumento da tag option tu tens fora da tag como texto <option ... [b]selected="selected"[/b]>...</option> IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted June 4, 2012 at 09:57 AM Author Report Share #460206 Posted June 4, 2012 at 09:57 AM olha bem para o que fizeste e o código que te foi fornecido: o código html selected="selected" à um argumento da tag option tu tens fora da tag como texto <option ... [b]selected="selected"[/b]>...</option> Já alterei mas continua sem dar não sei o que estou a fazer mal Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted June 4, 2012 at 10:00 AM Report Share #460207 Posted June 4, 2012 at 10:00 AM 1º - faz post do html resultante 2º - verifica se o id que estás a verificar realmente existe na lista a apresentar IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
pmg Posted June 4, 2012 at 10:00 AM Report Share #460208 Posted June 4, 2012 at 10:00 AM O ="selected" não faz parte da especificação HTML 4.1; nem da do HTML 5. Acho que faz parte da especifcação do XML no entanto. What have you tried? Não respondo a dúvidas por PM A minha bola de cristal está para compor; deve ficar pronta para a semana. Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código! Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted June 4, 2012 at 10:09 AM Report Share #460214 Posted June 4, 2012 at 10:09 AM O ="selected" não faz parte da especificação HTML 4.1; nem da do HTML 5. Acho que faz parte da especifcação do XML no entanto. estou habituado a escrever em XHTML ... IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted June 4, 2012 at 10:13 AM Author Report Share #460216 Posted June 4, 2012 at 10:13 AM está-me a dar um erro: a dizer que está à espera de um While na ultima linha: <?PHP include 'db_connect.php'; session_start(); if (isset($_GET['id'])) { $sel_id = "SELECT * FROM content_lang WHERE id='".$_GET['id']."'"; $ident = mysql_query($sel_id, $connect); $row_id = mysql_fetch_assoc($ident); $sel_ident="SELECT * FROM content"; $indentif=mysql_query($sel_ident, $connect); $row_ident=mysql_fetch_assoc($indentif); ?> <html> <head> <title>Administração - Editar</title> <script type="text/javascript" src="ckeditor/ckeditor.js"></script> <script src="ckeditor/_samples/sample.js" type="text/javascript"></script> <link href="ckeditor/_samples/sample.css" rel="stylesheet" type="text/css" /> </head> <body style="color:#FFFFFF;" bgcolor="#666666"> <form action="" method="post"> <table > <tr> <td align="right"> Identificador: </td> <td> <select name="identifier"> <?php do{ $sel_cont="SELECT * FROM content WHERE id_content='".$row_id['id_content']."'"; $cont=mysql_query($sel_cont, $connect); $row_cont=mysql_fetch_assoc($cont); $sel_cont_lang="SELECT * FROM content_lang WHERE id_content='".$row_cont['id_content']."'"; $cont_lang=mysql_query($sel_cont_lang, $connect); $row_cont_lang=mysql_fetch_assoc($cont_lang); ?> <option value="<?php echo $row_id['id_content'];?>" <?php if ($row_ident['id_content']==$row_id['id_content']){?> selected="selected" <?php;} ?>> <?PHP echo $row_ident['identifier']; ?> </option> <?PHP } while ($row_ident=mysql_fetch_assoc($indentif)); ?> </select> </td> </tr> <tr height="10px"></tr> <tr> <td align="right"> Linguagem: </td> <td> <select name="lang"> <option value="pt"> Português </option> <option value="en"> Inglês </option> </select> </td> </tr> <tr height="10px"></tr> <tr> <td align="right"> Título: </td> <td> <input value="<?php echo $row_cont_lang['titulo']; ?>" type="text" name="titulo"> </input> </td> </tr> <tr height="10px"></tr> <tr> <td align="right" valign="top"> Conteúdo: </td> <td> <textarea class="ckeditor" name="descricao" cols="50" rows="10"><?php echo $row_id['descricao']; ?></textarea> </td> </tr> <tr> <td></td> <td> <input type="submit" name="update" value="Editar Informação" /> <a href="admin.php"><input type="button" name="update" value="Voltar"/></a> </td> </tr> </table> </form> </body> </html> <?php if (isset($_POST['update'])) { $identifier=$_POST['identifier']; $lang=$_POST['lang']; $titulo=$_POST['titulo']; $descricao=$_POST['descricao']; $update_info="UPDATE content_lang SET id_content='".$identifier."', lang='".$lang."', titulo='".$titulo."', descricao='".$descricao."' WHERE id='".$row_id['id']."'"; $info=mysql_query($update_info, $connect); if (!$info) { echo '<script>alert("Erro na edição devido a um erro no sistem. Tente mais tarde"); location.href="admin.php"<script>'; } else { echo '<script>alert("Informação editada com sucesso."); location.href="admin.php";</script>'; } } } ?> Só agora é que deu este erro pq tava tudo bem...agora que alterei o codigo (penso que esteja a selecionar bem) dá-me isto :S Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted June 4, 2012 at 10:25 AM Report Share #460222 Posted June 4, 2012 at 10:25 AM é porque tens do do { } while() por fechar ... IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted June 4, 2012 at 10:29 AM Author Report Share #460224 Posted June 4, 2012 at 10:29 AM Pois mas é que eu tenho bem, só tenho um do{} while(): <select name="identifier"> <?php do{ $sel_cont="SELECT * FROM content WHERE id_content='".$row_id['id_content']."'"; $cont=mysql_query($sel_cont, $connect); $row_cont=mysql_fetch_assoc($cont); $sel_cont_lang="SELECT * FROM content_lang WHERE id_content='".$row_cont['id_content']."'"; $cont_lang=mysql_query($sel_cont_lang, $connect); $row_cont_lang=mysql_fetch_assoc($cont_lang); ?> <option value="<?php echo $row_id['id_content'];?>" <?php if ($row_ident['id_content']==$row_id['id_content']){?> selected="selected" <?php;} ?>> <?PHP echo $row_ident['identifier']; ?> </option> <?PHP } while ($row_ident=mysql_fetch_assoc($indentif)); ?> </select> Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted June 4, 2012 at 10:35 AM Report Share #460225 Posted June 4, 2012 at 10:35 AM como nunca programo neste modelo de salgalhada de php e html só posso aconselhar isto: <?php if ($row_ident['id_content']==$row_id['id_content']){?> selected="selected" <?php;} ?> separa correctamente os tags de php: <?php if ($row_ident['id_content']==$row_id['id_content']){ ?> selected="selected" <?php ;} ?> IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted June 4, 2012 at 10:46 AM Author Report Share #460227 Posted June 4, 2012 at 10:46 AM Já funciona... Obrigado pela ajuda, não conhecia a tag selected, por acaso..pq nunca tinha usado isto em php, mas sim em Visual Basic xD Cumprimentos, PF2G Link to comment Share on other sites More sharing options...
brunoais Posted June 4, 2012 at 11:48 AM Report Share #460243 Posted June 4, 2012 at 11:48 AM Obrigado pela ajuda, não conhecia a tag selected, por acaso..pq nunca tinha usado isto em php, mas sim em Visual Basic xD Só uma correção para evitar problemas. selected é um atributo, não uma tag. A tag é "option" que tem o atributo "selected" com o valor "selected". "[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31 Life is a genetically transmitted disease, induced by sex, with death rate of 100%. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now