Foros del Web » Programando para Internet » PHP »

Problema con bucle while

Estas en el tema de Problema con bucle while en el foro de PHP en Foros del Web. Hola. Vereis tengo el siguiente codigo Código PHP: <?php include( 'config.php' );  //Contiene los datos de la conexion a la BBDD //Este formulario enviara los datos a registrar.php, los comprobara, y si estan correctos, los introducira en la BBDD $anoactual  =  date ( "Y" ); echo '     <form action="registrar.php" method="POST">         Nombre: <input type="text" name="nombre" size="62" MAXLENGTH="30"><br>         Apellidos: <input type="text" name="apellidos" size="62" MAXLENGTH="50"><br> ...
  #1 (permalink)  
Antiguo 11/05/2011, 07:30
 
Fecha de Ingreso: abril-2011
Ubicación: En el mundo
Mensajes: 18
Antigüedad: 13 años, 1 mes
Puntos: 1
Problema con bucle while

Hola. Vereis tengo el siguiente codigo
Código PHP:
<?php
include('config.php'); //Contiene los datos de la conexion a la BBDD

//Este formulario enviara los datos a registrar.php, los comprobara, y si estan correctos, los introducira en la BBDD
$anoactual date("Y");
echo
'
    <form action="registrar.php" method="POST">
        Nombre: <input type="text" name="nombre" size="62" MAXLENGTH="30"><br>
        Apellidos: <input type="text" name="apellidos" size="62" MAXLENGTH="50"><br>
        Fecha de nacimiento: 
            Dia <SELECT NAME="dian" >
            <OPTION SELECTED>1<OPTION>2<OPTION>3<OPTION>4<OPTION>5<OPTION>6<OPTION>7<OPTION>8<OPTION>9<OPTION>10<OPTION>11<OPTION>12<OPTION>13<OPTION>14<OPTION>15<OPTION>16<OPTION>17<OPTION>18<OPTION>19<OPTION>20<OPTION>21<OPTION>22<OPTION>23<OPTION>24<OPTION>25<OPTION>26<OPTION>27<OPTION>28<OPTION>29<OPTION>30<OPTION>31
            </SELECT>
            Mes <SELECT NAME="mesn" >
            <OPTION SELECTED>1<OPTION>2<OPTION>3<OPTION>4<OPTION>5<OPTION>6<OPTION>7<OPTION>8<OPTION>9<OPTION>10<OPTION>11<OPTION>12
            </SELECT>
            Año <select name="ano">
            <option value="" selected>(Seleccionar)</option>
            do while($anoactual >= 1900){
            <option value="<'
.$anoactual.'>">'.$anoactual.'</option>
            '
.$anoactual.' = '.$anoactual.' - 1;
            };
            </select>
            <br>
        Nombre de Usuario: <input type="text" name="nick" size="62" MAXLENGTH="15"><br>
        Password: <input type="password" name="pass" size="62" MAXLENGTH="18"><br>
        Repetir Password: <input type="password" name="pass1" size="62" MAXLENGTH="18"><br>
        Email: <input type="text" name="email" size="62" MAXLENGTH="60"><br>
        <input type="submit" name="submit" value="Enviar">
    </form>
'
?>
Esto me imprime todos los datos que hay que rellenar en el foro para registrarse, pero tengo el problema, que en el campo del año, solo me aparece el año actual, y no los anteriores hasta 1900, y esto no se porque sucede. Me podeis echar un cable? Gracias :)
  #2 (permalink)  
Antiguo 11/05/2011, 08:18
Avatar de darkasecas  
Fecha de Ingreso: marzo-2005
Ubicación: SantaCata, NL, Mexico
Mensajes: 1.553
Antigüedad: 19 años, 1 mes
Puntos: 77
Respuesta: Problema con bucle while

Has visto el codigo html que genera ese codigo? >_>
No puedes colocar codigo para un ciclo dentro de un echo :| (bueno, si puedes, pero solo lo imprimira como texto, no lo ejecuta)

Ademas no tiene mucho setido colocar todo ese html dentro de un echo gigante, seria mejor ponerlo como html puro, y solo colocar dentro de las etiquetas de php el ciclo while
  #3 (permalink)  
Antiguo 11/05/2011, 08:41
Avatar de s00rk  
Fecha de Ingreso: octubre-2010
Ubicación: Mexico
Mensajes: 238
Antigüedad: 13 años, 6 meses
Puntos: 48
Respuesta: Problema con bucle while

Porque no usar un for y ya? n_n

Yo lo haria asi mira:
Código PHP:
<?php
include('config.php'); //Contiene los datos de la conexion a la BBDD

//Este formulario enviara los datos a registrar.php, los comprobara, y si estan correctos, los introducira en la BBDD
$anoactual date("Y"); 
?>
    <form action="registrar.php" method="POST">
        Nombre: <input type="text" name="nombre" size="62" MAXLENGTH="30"><br>
        Apellidos: <input type="text" name="apellidos" size="62" MAXLENGTH="50"><br>
        Fecha de nacimiento: 
            Dia <SELECT NAME="dian" >
            <OPTION SELECTED>1<OPTION>2<OPTION>3<OPTION>4<OPTION>5<OPTION>6<OPTION>7<OPTION>8<OPTION>9<OPTION>10<OPTION>11<OPTION>12<OPTION>13<OPTION>14<OPTION>15<OPTION>16<OPTION>17<OPTION>18<OPTION>19<OPTION>20<OPTION>21<OPTION>22<OPTION>23<OPTION>24<OPTION>25<OPTION>26<OPTION>27<OPTION>28<OPTION>29<OPTION>30<OPTION>31
            </SELECT>
            Mes <SELECT NAME="mesn" >
            <OPTION SELECTED>1<OPTION>2<OPTION>3<OPTION>4<OPTION>5<OPTION>6<OPTION>7<OPTION>8<OPTION>9<OPTION>10<OPTION>11<OPTION>12
            </SELECT>
            A&ntilde;o <select name="ano">
            <option value="" selected>(Seleccionar)</option>
            <? for($i $anoactual$i >= 1990$i--)
            {
?>
                <option value="<?=$i?>"><?=$i?></option>
               <? ?>
            </select>
            <br>
        Nombre de Usuario: <input type="text" name="nick" size="62" MAXLENGTH="15"><br>
        Password: <input type="password" name="pass" size="62" MAXLENGTH="18"><br>
        Repetir Password: <input type="password" name="pass1" size="62" MAXLENGTH="18"><br>
        Email: <input type="text" name="email" size="62" MAXLENGTH="60"><br>
        <input type="submit" name="submit" value="Enviar">
    </form>
  #4 (permalink)  
Antiguo 11/05/2011, 08:44
 
Fecha de Ingreso: febrero-2010
Mensajes: 295
Antigüedad: 14 años, 2 meses
Puntos: 58
Respuesta: Problema con bucle while

Hola otobusmzn.

darkasecas tiene razón, la parte de html debe de ir fuera del php en la medida de lo posible. Ponerlo dentro no tiene sentido, además que te dificulta mucho el código. La cosa sería así:

Código PHP:
<?php 
include('config.php'); //Contiene los datos de la conexion a la BBDD 

//Este formulario enviara los datos a registrar.php, los comprobara, y si estan correctos, los introducira en la BBDD 

?>


   <form action="registrar.php" method="POST"> 
        Nombre: <input type="text" name="nombre" size="62" MAXLENGTH="30"><br> 
        Apellidos: <input type="text" name="apellidos" size="62" MAXLENGTH="50"><br> 
        Fecha de nacimiento:  
            Dia <SELECT NAME="dian" > 
            <OPTION SELECTED>1<OPTION>2<OPTION>3<OPTION>4<OPTION>5<OPTION>6<OPTION>7<OPTION>8<OPTION>9<OPTION>10<OPTION>11<OPTION>12<OPTION>13<OPTION>14<OPTION>15<OPTION>16<OPTION>17<OPTION>18<OPTION>19<OPTION>20<OPTION>21<OPTION>22<OPTION>23<OPTION>24<OPTION>25<OPTION>26<OPTION>27<OPTION>28<OPTION>29<OPTION>30<OPTION>31 
            </SELECT> 
            Mes <SELECT NAME="mesn" > 
            <OPTION SELECTED>1<OPTION>2<OPTION>3<OPTION>4<OPTION>5<OPTION>6<OPTION>7<OPTION>8<OPTION>9<OPTION>10<OPTION>11<OPTION>12 
            </SELECT> 
            Año <select name="ano"> 
            <option value="" selected>(Seleccionar)</option> 
            <?php
             
for ($anoactual=date('Y'); $anoactual>=1900$anoactual--){ 
               echo 
"<option value='$anoactual'>".$anoactual."</option>";
            
            }; 
            
?>
            </select> 
            <br> 
        Nombre de Usuario: <input type="text" name="nick" size="62" MAXLENGTH="15"><br> 
        Password: <input type="password" name="pass" size="62" MAXLENGTH="18"><br> 
        Repetir Password: <input type="password" name="pass1" size="62" MAXLENGTH="18"><br> 
        Email: <input type="text" name="email" size="62" MAXLENGTH="60"><br> 
        <input type="submit" name="submit" value="Enviar"> 
    </form>
El php lo pones solo donde sea necesario. Lo demás lo dejas como html.
Te he cambiado el bucle while por un bucle for, pero es lo mismo.
Para el día y el mes puedes hacer bucles parecidos. No es necesario que pongas lo que has puesto. Aunque ya lo tienes hecho déjalo, pero yo te recomendaría que lo intentaras hacer con bucles para practicar.

Un saludo.

EDITO: s00rk se me ha adelantado XD

Etiquetas: bucle, fecha, select
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 06:02.