Problema con función recursiva versus ARRAY
| From: | Conocimiento Virtual | Date: | Wed, 22 May 2002 01:27:44 +0000 |
| Subject: | Problema con función recursiva versus ARRAY | ||
| Groups: | php.general.es | ||
| Request: | Send a blank email to general-es+get-22914@lists.php.net to get a copy of this message | ||
Hola amigos.
Tengo un problema que me trae "Del Pelo"... a ver si me pueden ayudar:
Hice una función recursiva para agrupar un resultado de un query (recordset) y me funciona
perfecto, he aquí la funcion:
--------------------------------------------------------
/*
Funcion para agrupar jerárquicamente el resultado de un query
$ia = itemes a agrupar
$ip = itemes a pintar
$Rs = Nombre del Recordset
$agrup = valor del item agrupador
*/
function AgruparRs($ia, $ip, $Rs,$agrup)
{ while (odbc_fetch_row($Rs)) /* NO SE ACABE EL RECORDSET */
{ static $i=1; /* VARIABLE QUE SOLO TOMA EL VALOR LA PRIMERA VEZ */
$item = odbc_result($Rs,$i);
/* PREGUNTO SI EL ITEM NUEVO ES DIFERENTE AL AGRUPADOR PARA PINTARLO */
if ($item <> $agrup)
{ echo $item ."<BR>";
$agrup = $item;
$i++;
}
else /* SI ES IGUAL, NO LO PINTO PERO SI AUMENTO EL i */
{ $i++;
}
/* PREGUNTO SI FALTA POR AGRUPAR TODAVIA Y VUELVO A LLAMAR LA FUNCION */
if ($i <= $ia)
{ AgruparRs($ia, $ip, $Rs, $agrup);
}
else /* SINO, PINTO LOS DEMAS CAMPOS */
{ echo "<img src=images/blank.gif width=10>";
for ($k=$i; $k <= $ip; $k++)
{ $item=(odbc_result($Rs,$k));
echo $item ." | ";
}
}
$i--;
} /* fin del while */
} /* fin de la funcion */
--------------------------------------------------------
Asi, si tengo un resultado de un query como esto:
TEMA TIPOPRENDA REF. TALLA COLOR CANTIDAD
bangalore Pantalon df2207--24 006 Indigo medio 3
bangalore Pantalon df2207--24 010 Indigo claro 44
Y le aplico la funcion con dos niveles de agrupacion, la funcion me pinta esto:
Bangalore
Pantalon
df2207--24 | 006 | Indigo medio | 3
df2207--24 | 010 | Indigo claro | 44
EL PROBLEMA QUE TENGO ES: Si en vez de un Recordset, le paso un array como parametro (y ajusto el
while por un foreach para que lo recorra), cuando lo agrupo por 2 niveles me pinta esto:
Bangalore
Pantalon
df2207--24 | 006 | Indigo medio | 3
df2207--24 | 010 | Indigo claro | 44
Pantalon
df2207--24 | 006 | Indigo medio | 3
df2207--24 | 010 | Indigo claro | 44
Es decir me pinta dos veces el segundo grupo...
Alguien sabe qué puedo hacer para solucinarlo??
Gracias por leer semejante mensaje y por su ayuda.
Vik! ®
pd: ADJUNTO LA FUNCION CON ARRAY EN VEZ DE CON RECORDSET
--------------------------------------------
function AgruparArray($ia, $ip, $Arr,$agrup)
while (list (,$element) = each ($Arr)) /* MIENTRAS NO SE ACABE EL ARRAY */
{ static $i=1; /* VARIABLE QUE SOLO TOMA EL VALOR LA PRIMERA VEZ */
$item = $element[$i];
/* PREGUNTO SI EL ITEM NUEVO ES DIFERENTE AL AGRUPADOR PARA PINTARLO */
if ($item <> $agrup)
{ echo $item ."<BR>";
$agrup = $item;
$i++;
}
else /* SI ES IGUAL, NO LO PINTO PERO SI AUMENTO EL i */
{ $i++;
}
/* PREGUNTO SI FALTA POR AGRUPAR TODAVIA Y VUELVO A LLAMAR LA FUNCION */
if ($i <= $ia)
{ AgruparRs($ia, $ip, $Rs, $agrup);
}
else /* SINO, PINTO LOS DEMAS CAMPOS */
{ echo "<img src=images/blank.gif width=10>";
for ($k=$i; $k <= $ip; $k++)
{ $item = $element[$k];
echo $item ." | ";
}
}
$i--;
} /* fin del while */
} /* fin de la funcion */