Foros del Web » Programando para Internet » PHP »

Problemas con select

Estas en el tema de Problemas con select en el foro de PHP en Foros del Web. Hola amigos tengo un problema comun les explico tengo un gestor de Noticias que insertar muy bien la noticia por categorias asi @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código ...
  #1 (permalink)  
Antiguo 14/09/2009, 15:55
(Desactivado)
 
Fecha de Ingreso: diciembre-2008
Mensajes: 421
Antigüedad: 15 años, 4 meses
Puntos: 0
Problemas con select

Hola amigos tengo un problema comun les explico

tengo un gestor de Noticias que insertar muy bien la noticia por categorias asi
Código html:
Ver original
  1. <td>Categoria:</td>
  2.           <td>
  3.          <SELECT name="categoria">
  4.    
  5.          <OPTION VALUE="General">General</OPTION>
  6.          <OPTION VALUE="sub7">Sub 7</OPTION>
  7.          <OPTION VALUE="sub9">Sub 9</OPTION>
  8.          <OPTION VALUE="sub11">Sub 11</OPTION>
  9.          <OPTION VALUE="sub13">Sub 13</OPTION>
  10.          <OPTION VALUE="sub15">Sub 15</OPTION>
  11.          <OPTION VALUE="sub17">Sub 17</OPTION>
  12.          <OPTION VALUE="sub19">Sub 19</OPTION>
  13.          <OPTION VALUE="primera">Primera "A"</OPTION>
  14.          <OPTION VALUE="primerab">Primera "B"</OPTION>
  15.          <OPTION VALUE="primeraAS">Primeras Ascenso</OPTION>
  16.          <OPTION VALUE="segundaAS">Segundas Ascenso</OPTION>
  17.          <OPTION VALUE="femenino">Femenino </OPTION>
  18.          <OPTION VALUE="mutual">Mutual</OPTION>
  19.           </td>

Bien el problema pasa por lo siguiente siempre cuando pongo actualizar o editar alguna noticias deberia aparecerme en este mismo select la categoria que he seleccionado pero no es asi, me aparece directamente la opcion general. Es ahi donde nuevamente tengo que seleccionar la categoria y genero el error ¿Alguien sabe como deberia hacer para que cuando ponga editar me salga en mi select la categoria seleccionada y no pase directamente a general?
  #2 (permalink)  
Antiguo 14/09/2009, 16:00
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 11 meses
Puntos: 1517
Respuesta: Problemas con select

Mira este post, leelo hasta el final para que tengas una idea de lo que debes hacer http://www.forosdelweb.com/f18/como-...e-este-725012/
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos
  #3 (permalink)  
Antiguo 14/09/2009, 16:19
(Desactivado)
 
Fecha de Ingreso: diciembre-2008
Mensajes: 421
Antigüedad: 15 años, 4 meses
Puntos: 0
Respuesta: Problemas con select

No entiendo muy bien lo que debo hacer o no me he dejado entender
hice esto como dices
Código php:
Ver original
  1. <td>Categoria:</td>
  2.           <td>
  3.           <?php
  4.          
  5.            while ($row = pg_fetch_array($result)) {
  6.            $s = ($row["categoria"]  "selected" : ""]);
  7.            echo "<option value='".$row["categoria"]."' ".$s.">".$row["categoria"]."</option>";
  8.           }
  9.    
  10.         ?>
  11.    
  12.       </select>
Pero no es la solucion
Solo quiero que cuando haga editar me muestre la categoria que habia seleccionado y no pase directamente a general
  #4 (permalink)  
Antiguo 14/09/2009, 16:24
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 11 meses
Puntos: 1517
Respuesta: Problemas con select

Toma este ejemplo para que tengas una idea. Debes tener en cuenta primero que requieres pasar el select a una base de datos para que funcione lo que te estoy diciendo. O colocarlos en un array y llamarlos en un loop
Código php:
Ver original
  1. $query1 = mysql_query("SELECT * FROM categoria") or die(mysql_error());
  2. $query2 = mysql_query("SELECT * FROM tabla_que_tiene_info_del_usuario WHERE id = '".$id_del_usuario."'") or die(mysql_error());
  3. $row2 = mysql_fetch_assoc($query2);
  4. while ($row1 = mysql_fetch_assoc($query1)) {
  5.   $s = ($row1["nombre_de_columna"] == $row2["columna_que_tiene_el_estado_que_selecciono"] ? "selected" : "");
  6.   echo "<option value='".$row1["nombre_de_columna"]."' ".$s.">".$row1["nombre_de_columna"]."</option>";
  7. }
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos
  #5 (permalink)  
Antiguo 14/09/2009, 16:44
(Desactivado)
 
Fecha de Ingreso: diciembre-2008
Mensajes: 421
Antigüedad: 15 años, 4 meses
Puntos: 0
Sigo sin poder entender me guie en tu ejemplo pero la idea en q en mi tabka noticias es un campo categorias y al momento de editar pongo tu codigo de ejemplo con algunas modificaciones tengo serio errores cono este
Código php:
Ver original
  1. <td>Categoria:</td>
  2.           <td>
  3.           <?php
  4.           $query1 = mysql_query("SELECT * FROM noticia") or die(mysql_error());
  5.            $row2 = mysql_fetch_assoc($query2);
  6.           while ($row1 = mysql_fetch_assoc($query1)) {
  7.             $s = ($row1["categoria"] == $row2["categoria"] ? "selected" : "");
  8.             echo "<option value='".$row1["categoria"]."' ".$s.">".$row1["categoria"]."</option>";
  9. }
  10.  
  11.          
  12.          
  13.           ?>
  14.           </td>
  15. El error
  16.    
  17. Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\proyecto_cisco\modulos\noticias\vistas\editar.html.php on line 51
  18. sub8primerasub8femeninomutualsub20sub15sub12General

No entiendo como deberia ser para que cuando edite alguna noticia en mi select me aparezca o me muestre la categoria con la que habia insertado la noticia y no me muestra directamente la categoria general hice esto: dentro del select
Código php:
Ver original
  1. <?php
  2. include('conectar.php');
  3. $link = Conectarse();
  4. $id = $_GET['id'];
  5. $result=mysql_query("Select * From noticia WHERE id='$id' LIMIT 1");
  6. $row=mysql_fetch_array($result);
  7. ?>
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  11. <title>Insertar Noticias </title>
  12. </head>
  13. <body>
  14. <table border="0" cellspacing="0" cellpadding="0" width="500" align="center">
  15.     <tr>
  16.    
  17.       <form action="index.php?mod=noticias&pag=editar_1" method="post" name="frmNoticia" >
  18.        <input name="id" type="hidden" value="<?php echo $row['id']; ?>" />
  19.      
  20.         <table width="90%" border="1">
  21.        
  22.           <tr>
  23.             <td>Noticia:</td>
  24.             <td><textarea name="txt_textocompleto" cols="30" class="mceEditor"><?php echo $row['texto_completo']; ?> </textarea>
  25.             </td>
  26.           </tr>
  27.          
  28.           <tr>
  29.             <td>Estado de Publicacion:</td>
  30.             <td><input type="checkbox" name="chbx_estado" value="1" <?php if($row['estado']==1){echo "checked=checked"; }?>/>(Si la Noticia será Publicada )
  31.             </td>
  32.           </tr>
  33.           <tr>
  34.           <td>Categoria:</td>
  35.           <td>
  36.           <?php
  37.            
  38.             $query1 = mysql_query("SELECT * FROM noticia") or die(mysql_error());
  39.             $query2 = mysql_query("SELECT * FROM noticia WHERE id = '$id '") or die(mysql_error());
  40.             //$result=mysql_query("Select * From noticia WHERE id='$id' LIMIT 1");
  41.             $row2 = mysql_fetch_assoc($query2);
  42.             while ($row1 = mysql_fetch_assoc($query1)) {
  43.               $s = ($row1["categoria"] == $row2["categoria"] ? "selected" : "");
  44.               echo "<option value='".$row1["categoria"]."' ".$s.">".$row1["categoria"]."</option>";
  45.             }
  46.  
  47.           ?>
  48.           <OPTION VALUE="General">General</OPTION>
  49.          <OPTION VALUE="sub7">Sub 7</OPTION>
  50.          <OPTION VALUE="sub9">Sub 9</OPTION>
  51.          <OPTION VALUE="sub11">Sub 11</OPTION>
  52.          <OPTION VALUE="sub13">Sub 13</OPTION>
  53.          <OPTION VALUE="sub15">Sub 15</OPTION>
  54.          <OPTION VALUE="sub17">Sub 17</OPTION>
  55.          <OPTION VALUE="sub19">Sub 19</OPTION>
  56.          <OPTION VALUE="primera">Primera "A"</OPTION>
  57.          <OPTION VALUE="primerab">Primera "B"</OPTION>
  58.          <OPTION VALUE="primeraAS">Primeras Ascenso</OPTION>
  59.          <OPTION VALUE="segundaAS">Segundas Ascenso</OPTION>
  60.          <OPTION VALUE="femenino">Femenino </OPTION>
  61.          <OPTION VALUE="mutual">Mutual</OPTION>
  62. </SELECT>
  63.           </td>
  64.           </tr>
  65.  
  66. </table>
  67.  
  68. </body>
  69. </html>
Pero lo unico que logro cuando edito una noticia es que me muestra todos los campos correctos pero ahora en la parte del select me sale esto:
sub8primerasub8femeninomutualsub20sub15sub12Genera l General Sub 7 Sub 9 Sub 11 Sub 13 Sub 15 Sub 17 Sub 19 Primera "A" Primera "B" Primeras Ascenso Segundas Ascenso Femenino Mutual

Ya puede hacer todo lo logico para que en mi select option me muestre la categoria que habia seleccionado por medio de este duda:
http://www.forosdelweb.com/f18/edita...eccion-722200/

Pero hice esto
Código php:
Ver original
  1. <td>Categoria:</td>
  2.           <td>
  3.          
  4.          
  5.         <SELECT NAME='categoria'>
  6.         <OPTION value ="General" <?php if ($row['categoria'] == 'General') { echo "selected"; } ?>>General</OPTION>
  7.         <OPTION value ="sub7" <?php if ($row['categoria'] == 'Sub7') { echo "selected"; } ?>>Sub 7</OPTION>
  8.         <OPTION value ="sub9" <?php if ($row['categoria'] == 'Sub9') { echo "selected"; } ?>>Sub 9</OPTION>
  9.         <OPTION value ="sub11"<?php if ($row['categoria'] == 'Sub11') { echo "selected"; } ?>>Sub 11</OPTION>
  10.         </SELECT >
  11.  
  12.        <?php echo $row['categoria']; ?>
  13.           </td>
  14.           </tr>

Cuando hago click en editar me muestra todos los datos de texto y en <?php echo $row['categoria']; ?> me imprime correctamente la categoria que habia sleccionado pero no me muestra dentro de mi select esa categoria ¿En que estoy fallando ?

Última edición por GatorV; 15/09/2009 a las 11:02
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:26.