Foros del Web » Programando para Internet » PHP »

Problema con whiles anidados

Estas en el tema de Problema con whiles anidados en el foro de PHP en Foros del Web. Tengo dos whiles de dos consultas diferentes anidados, el problema es que solo me ejecuta el ciclo interior Mi codigo: Código PHP: while( $CONTAINER  =  ...
  #1 (permalink)  
Antiguo 09/01/2004, 12:33
 
Fecha de Ingreso: marzo-2002
Ubicación: Guadalajara, Jalisco.
Mensajes: 213
Antigüedad: 22 años, 1 mes
Puntos: 0
Problema con whiles anidados

Tengo dos whiles de dos consultas diferentes anidados, el problema es que solo me ejecuta el ciclo interior

Mi codigo:

Código PHP:
while($CONTAINER mysql_fetch_array($containerq))
     {
      while(
$CHAR mysql_fetch_array($charges))
       {
        if(${
'chargepp' $CHAR[0] . 'cont' $CONTAINER[0]} == "on")
         {
          
$idchar mysql_query("SELECT id_charge, ch_type FROM `toctrack_charges` WHERE `ch_type` = '1' ORDER BY id_charge DESC LIMIT 1") or die("Invalid query: " mysql_error());
          
$CHARGEID =  mysql_fetch_array($idchar);
          
mysql_query("INSERT INTO `toctrack_chargein` (`id_charges`, `id_cat_charges`)
          VALUES ('$CHARGEID[0]', '$CHAR[0]')"
) or die("Invalid query: " mysql_error());
         }
        else if(${
'chargesp' $CHAR[0] . 'cont' $CONTAINER[0]} == "on")
         {
          echo 
"asdfñjdsafñlñfjdfklñjdsa";
          
$idchar2 mysql_query("SELECT id_charge, ch_type FROM `toctrack_charges` WHERE `ch_type` = '2' ORDER BY id_charge DESC LIMIT 1") or die("Invalid query: " mysql_error());
          
$CHARGEID2 =  mysql_fetch_array($idchar2);
          
mysql_query("INSERT INTO `toctrack_chargein` (`id_charges`, `id_cat_charges`)
          VALUES ('$CHARGEID2[0]', '$CHAR[0]')"
) or die("Invalid query: " mysql_error());
         }
       }
     } 
Espero que me puedan ayudar.

Saludos!
__________________
Edgar Castro
Estudiante de Ingenieria en Sistemas @ ITESO
http://www.iteso.mx
  #2 (permalink)  
Antiguo 09/01/2004, 15:47
Ex Colaborador
 
Fecha de Ingreso: junio-2002
Mensajes: 9.091
Antigüedad: 21 años, 10 meses
Puntos: 16
Hola,

Yo creo que se ejecutan los dos. Pero el segundo solo se ejecuta como quieres la primera vez.

Tal como lo tienes, por cada ciclo del while exterior, ejecuta todos los ciclos del while interior. Lo que pasa es que el while interior recorrer todo el recordset la primera iteraccion del while exterior. Por eso, cuando en la segunda iteracion del exterior, vuelve a ejecutar el interior, resulta que la llamada a mysql_fetch_array() devuelve false porque en la primera vez ya llego hasta el fin.

No se si me he explicado.

La solucion es resetear el recordset del while interior cada vez que se ejecutan cada ciclo del while exterior. Algo como:
Código PHP:
while($CONTAINER mysql_fetch_array($containerq))

     {

      while(
$CHAR mysql_fetch_array($charges))

       {

        if(${
'chargepp' $CHAR[0] . 'cont' $CONTAINER[0]} == "on")

         {

          
$idchar mysql_query("SELECT id_charge, ch_type FROM `toctrack_charges` WHERE `ch_type` = '1' ORDER BY id_charge DESC LIMIT 1") or die("Invalid query: " mysql_error());

          
$CHARGEID =  mysql_fetch_array($idchar);

          
mysql_query("INSERT INTO `toctrack_chargein` (`id_charges`, `id_cat_charges`)

          VALUES ('$CHARGEID[0]', '$CHAR[0]')"
) or die("Invalid query: " mysql_error());

         }

        else if(${
'chargesp' $CHAR[0] . 'cont' $CONTAINER[0]} == "on")

         {

          echo 
"asdfñjdsafñlñfjdfklñjdsa";

          
$idchar2 mysql_query("SELECT id_charge, ch_type FROM `toctrack_charges` WHERE `ch_type` = '2' ORDER BY id_charge DESC LIMIT 1") or die("Invalid query: " mysql_error());

          
$CHARGEID2 =  mysql_fetch_array($idchar2);

          
mysql_query("INSERT INTO `toctrack_chargein` (`id_charges`, `id_cat_charges`)

          VALUES ('$CHARGEID2[0]', '$CHAR[0]')"
) or die("Invalid query: " mysql_error());

         }

       }

       
mysql_data_seek($charges,0);

     } 
(www.php.net/mysql_data_seek)

Con esto conseguiras recorrer el recordset interior de principio a fin en para cada registro del recordset exterior.

Suerte.
__________________
Josemi

Aprendiz de mucho, maestro de poco.
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.
Tema Cerrado

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 09:32.