Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/12/2011, 07:47
controler
 
Fecha de Ingreso: mayo-2011
Mensajes: 11
Antigüedad: 12 años, 10 meses
Puntos: 0
Simplificar codigo php

Saludos. He conseguido generar un menú en árbol con checkboxes de hasta 6 niveles y cogiendo los datos de una BBDD Postgresql. Me ha quedado algo largo y confuso, con varias sentencias repetidas a lo largo de los bucles for anidados. A parte del numero de variables que he tenido que crear. Estoy seguro que pudo simplificarlo por medio de funciones propias, pero no consigo hacerme a la idea de como estructurarlo y lo único que obtengo son fallos en las funciones y al llamarlas. Este es el código tal cual me funciona:

Código PHP:
for ($b=0;$b<count($nivel1gmao);$b++){    //bucle que recorra los elementos del nivel
    
if($nivel1gmao[$b] != null){ //Solo ejecute  valores NO nulos
        
echo '<li><input type="checkbox"/><label>'.$nivel1gmao[$b].'</label><ul>'//Imprime los elementos del nivel
        //Comienzo del nivel 2
        
$selec pg_query($dbconn"select distinct nivel2gmao from inventario where nivel1gmao='".$nivel1gmao[$b]."'"); //En cada pasada del bucle hacemos la solicitud de los elementos hijos de cada elemento del nivel padre
        
while ($selecarr pg_fetch_array($selec,NULL,PGSQL_BOTH)){ $nivel_1[$i] = $selecarr['nivel2gmao'];$i++;}$i=0;
        for(
$c=0;$c<count($nivel_1);$c++){
            echo 
'<li><input type="checkbox"/><label>'.$nivel_1[$c].'</label><ul>';
                    
//Comienzo del nivel 3
                    
$selec pg_query($dbconn"select distinct nivel3gmao from inventario where nivel2gmao='".$nivel_1[$c]."'"); 
                    while (
$selecarr pg_fetch_array($selec,NULL,PGSQL_BOTH)){ $nivel_2[$i] = $selecarr['nivel3gmao'];$i++;}$i=0;
                    for(
$e=0;$e<count($nivel_2);$e++){
                        if(
$nivel_2 != null){
                            echo 
'<li><input type="checkbox"/><label>'.$nivel_2[$e].'</label><ul>';
                                
//comienzo del nivel 4
                                
$selec pg_query($dbconn"select distinct nivel4gmao from inventario where nivel3gmao='".$nivel_2[$e]."'"); 
                                while (
$selecarr pg_fetch_array($selec,NULL,PGSQL_BOTH)){ $nivel_3[$i] = $selecarr['nivel4gmao'];$i++;}$i=0;
                                for(
$f=0;$f<count($nivel_3);$f++){
                                    if(
$nivel_3[$f] != null){
                                        echo 
'<li><input type="checkbox"><label>'.$nivel_3[$f].'</label><ul>';
                                        
//Comienzo del nivel 5
                                        
$selec pg_query($dbconn"select distinct nivel5gmao from inventario where nivel4gmao='".$nivel_3[$f]."'");
                                        while (
$selecarr pg_fetch_array($selec,NULL,PGSQL_BOTH)){ $nivel_4[$i] = $selecarr['nivel5gmao'];$i++;}$i=0;
                                        for(
$h=0;$h<count($nivel_4);$h++){
                                            if(
$nivel_4[$h] != null){
                                                echo 
'<li><input type="checkbox"><label>'.$nivel_4[$h].'</label><ul>';
                                                
//Comienzo del nivel 6 que muestra los elementos
                                                
$selec pg_query($dbconn"select distinct codigocampo from inventario where nivel5gmao='".$nivel_4[$h]."'"); 
                                                while (
$selecarr pg_fetch_array($selec,NULL,PGSQL_BOTH)){ $nivel_5[$i] = $selecarr['codigocampo'];$i++;}$i=0;
                                                for(
$k=0;$k<count($nivel_5);$k++){
                                                    
                                                        echo 
'<li><input type="checkbox"><label>'.$nivel_5[$k].'</label>';
                                                    
                                                }unset(
$nivel_5);
                                            echo 
"</ul>";    
                                            }
//-------
                                            
else{
                                                
$selec pg_query($dbconn"select distinct codigocampo from inventario where (nivel4gmao='".$nivel_3[$f]."' AND nivel5gmao is null)"); 
                                                while (
$selecarr pg_fetch_array($selec,NULL,PGSQL_BOTH)){ $final2[$i] = $selecarr['codigocampo'];$i++;}$i=0;
                                                for(
$j=0;$j<count($final2);$j++){
                                                    if(
$final2[$j] != null){
                                                        echo 
'<li><input type="checkbox"><label>'.$final2[$j].'</label>';
                                                    }
                                                }unset(
$final2);    
                                            }
                                        }unset(
$nivel_4);
                                        echo 
"</ul>";
                                    }
                                    else{
                                        
$selec pg_query($dbconn"select codigocampo from inventario where (nivel3gmao='".$nivel_2[$e]."' AND nivel4gmao is null)"); 
                                        while (
$selecarr pg_fetch_array($selec,NULL,PGSQL_BOTH)){ $final[$i] = $selecarr['codigocampo'];$i++;}$i=0;
                                        for(
$g=0;$g<count($final);$g++){
                                            if(
$final[$g] != null){
                                                echo 
'<li><input type="checkbox"><label>'.$final[$g].'</label>';
                                            }
                                        }unset(
$final);
                                    }
                                }unset(
$nivel_3);
                                echo 
"</ul>";
                        }
                    }unset (
$nivel_2);                                    
            echo 
"</ul>";            
        } unset (
$nivel_1);
        echo 
"</ul>";    
    }
}echo 
"</ul>";
?> 
Esta es una captura del menu:



Me sería de gran ayuda. Gracias

Última edición por controler; 20/12/2011 a las 07:55 Razón: Añadir imagen de muestra