Foros del Web » Programando para Internet » PHP »

error en foreach

Estas en el tema de error en foreach en el foro de PHP en Foros del Web. Hola tengo el siguiente codigo que me genera este error Warning: Invalid argument supplied for foreach() in /home/kwmitcom/public_html/test/GP1.php on line 41 mi linea 41 es ...
  #1 (permalink)  
Antiguo 31/10/2013, 10:15
 
Fecha de Ingreso: octubre-2013
Mensajes: 79
Antigüedad: 10 años, 6 meses
Puntos: 0
Pregunta error en foreach

Hola tengo el siguiente codigo que me genera este error


Warning: Invalid argument supplied for foreach() in /home/kwmitcom/public_html/test/GP1.php on line 41


mi linea 41 es esta!

Código PHP:
        foreach($Fechas1 as $i => $FechaG) { 
mi linea 42es esta!

Código PHP:
        fwrite($fp,"\t\t\t\t\t\t$FechaG\t{$Locali[$i]}\t{$TypeG[$i]}\t{$Moneda[$i]}\t{$Subto[$i]}\t{$Porcentaje[$i]}\t{$Iva[$i]}\t {$Otherim[$i]}\t{$Total[$i]}\t{$Just[$i]}\t{$RFC[$i]}\t{$Factura[$i]}\t{$Proveedor[$i]}\t{$Tpago[$i]}"PHP_EOL);}
        
fclose($fp); 
ayuda por favor
  #2 (permalink)  
Antiguo 31/10/2013, 10:45
Avatar de Crazylegs  
Fecha de Ingreso: septiembre-2013
Ubicación: Barcelona
Mensajes: 74
Antigüedad: 10 años, 6 meses
Puntos: 14
Respuesta: error en foreach

Hola, prueba antes de la linea 41 poner:

Código PHP:
Ver original
  1. print_r($Fechas1);

para ver que contiene esa variable.

Enséñanos lo que te printa el print_r.
__________________
¡Mira mis tutoriales web!
  #3 (permalink)  
Antiguo 31/10/2013, 10:50
Colaborador
 
Fecha de Ingreso: septiembre-2013
Ubicación: España
Mensajes: 3.648
Antigüedad: 10 años, 7 meses
Puntos: 578
Respuesta: error en foreach

Yo comprobaría si $Fechas1 es un array:

Código PHP:
Ver original
  1. echo if (is_array($Fechas1)) // == false xD
  #4 (permalink)  
Antiguo 31/10/2013, 11:29
 
Fecha de Ingreso: octubre-2013
Mensajes: 79
Antigüedad: 10 años, 6 meses
Puntos: 0
Respuesta: error en foreach

OK, puse el codigo que me indicaron pero sigue la falla y no me imprime nada en pantalla, les dejo mi codigo completo no tengo ni idea que sea y llevo dias lidiando :D muchas gracias por la ayuda.

Código PHP:

        $fp 
fopen("Capturas/Administracion/Admon.xls","a");
        
fwrite($fp,""PHP_EOL);
        
fwrite($fp"$fecha_actual\t$Nombre\t$Depto\t$Proyect\t$LapsoI\t$LapsoF \t FECHA \t LOCALIDAD \t CONCEPTO \t MONEDA \t SUBTOTAL \t PORCENTAJE \t IVA \t OTROS \t TOTAL \t JUSTIFICACION \t RFC \t FACTURA \t PROVEEDOR \t T/PAGO \t $folio" PHP_EOL);
        
$array = array(
                array(
'FechaG''Locali''TypeG''Moneda''Subto''Porcentaje''Iva''Total''Just''RFC''Factura''Proveedor''Tpago'), // numérico
                
array('$FechaG' => 'valor FechaG''$Locali' => 'valor Locali''$TypeG' => 'valor TypeG''$Moneda' => 'valor Moneda''$Subto' => 'valor Subto''$Porcentaje' => 'valor Porcentaje''$Iva' => 'valor Iva''$Total' => 'valor Otherim''$Otherim' => 'valor Total''$Just' => 'valor Just''$RFC' => 'valor RFC''$Factura' => 'valor Factura''$Proveedor' => 'valor Proveedor''$Tpago' => 'valor Tpago'));
        foreach(
$Fechas1 as $i => $FechaG
        {
        
fwrite($fp,"\t\t\t\t\t\t$FechaG\t{$Locali[$i]}\t{$TypeG[$i]}\t{$Moneda[$i]}\t{$Subto[$i]}\t{$Porcentaje[$i]}\t{$Iva[$i]}\t {$Otherim[$i]}\t{$Total[$i]}\t{$Just[$i]}\t{$RFC[$i]}\t{$Factura[$i]}\t{$Proveedor[$i]}\t{$Tpago[$i]}"PHP_EOL);
        }
        
fclose($fp); 
  #5 (permalink)  
Antiguo 31/10/2013, 11:41
Avatar de Crazylegs  
Fecha de Ingreso: septiembre-2013
Ubicación: Barcelona
Mensajes: 74
Antigüedad: 10 años, 6 meses
Puntos: 14
Respuesta: error en foreach

Comenta las lineas donde tira el fallo, es decir, el foreach y pon el print_r:

Código PHP:
Ver original
  1. $fp = fopen("Capturas/Administracion/Admon.xls","a");
  2.         fwrite($fp,"". PHP_EOL);
  3.         fwrite($fp, "$fecha_actual\t$Nombre\t$Depto\t$Proyect\t$LapsoI\t$LapsoF \t FECHA \t LOCALIDAD \t CONCEPTO \t MONEDA \t SUBTOTAL \t PORCENTAJE \t IVA \t OTROS \t TOTAL \t JUSTIFICACION \t RFC \t FACTURA \t PROVEEDOR \t T/PAGO \t $folio" . PHP_EOL);
  4.         $array = array(
  5.                 array('FechaG', 'Locali', 'TypeG', 'Moneda', 'Subto', 'Porcentaje', 'Iva', 'Total', 'Just', 'RFC', 'Factura', 'Proveedor', 'Tpago'), // numérico
  6.                 array('$FechaG' => 'valor FechaG', '$Locali' => 'valor Locali', '$TypeG' => 'valor TypeG', '$Moneda' => 'valor Moneda', '$Subto' => 'valor Subto', '$Porcentaje' => 'valor Porcentaje', '$Iva' => 'valor Iva', '$Total' => 'valor Otherim', '$Otherim' => 'valor Total', '$Just' => 'valor Just', '$RFC' => 'valor RFC', '$Factura' => 'valor Factura', '$Proveedor' => 'valor Proveedor', '$Tpago' => 'valor Tpago'));
  7.         print_r($Fechas1);
  8.         /*foreach($Fechas1 as $i => $FechaG)
  9.         {
  10.         fwrite($fp,"\t\t\t\t\t\t$FechaG\t{$Locali[$i]}\t{$TypeG[$i]}\t{$Moneda[$i]}\t{$Subto[$i]}\t{$Porcentaje[$i]}\t{$Iva[$i]}\t {$Otherim[$i]}\t{$Total[$i]}\t{$Just[$i]}\t{$RFC[$i]}\t{$Factura[$i]}\t{$Proveedor[$i]}\t{$Tpago[$i]}". PHP_EOL);
  11.         }*/
  12.         fclose($fp);
__________________
¡Mira mis tutoriales web!
  #6 (permalink)  
Antiguo 31/10/2013, 12:16
 
Fecha de Ingreso: octubre-2013
Mensajes: 79
Antigüedad: 10 años, 6 meses
Puntos: 0
Respuesta: error en foreach

Ya lo agregue como me indicaste y no me aparece nada! solo que ya no me tira el error

la variable Fechas1 es esta

Código PHP:
$Fechas1 $_POST['FechaG']; 
  #7 (permalink)  
Antiguo 31/10/2013, 12:21
 
Fecha de Ingreso: septiembre-2011
Mensajes: 219
Antigüedad: 12 años, 7 meses
Puntos: 31
Respuesta: error en foreach

Cita:
Iniciado por linuxchucho Ver Mensaje
Ya lo agregue como me indicaste y no me aparece nada! solo que ya no me tira el error

la variable Fechas1 es esta

Código PHP:
$Fechas1 $_POST['FechaG']; 
Ahí tienes el error, $Fechas1 viene vacío, por lo tanto foreach te va a dar error.
Tienes dos opciones, si $Fechas1 debería si o si traer datos, rastrea porque no lo hace. Y si $Fechas1 en algunos casos puede venir vacío, agrega una validación antes de lanzar el foreach.

Saludos

Etiquetas: foreach, html
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 01:28.