Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    Come assegnare id univoco tramite funzione ricorsiva?

    Buon pomeriggio, ho la necessità di dover assegnare un id univoco ai link di un menu ad albero che strutturo attraverso una funzione ricorsiva.
    Praticamente, attraverso una path di una cartella contenente, cartelle, sottocartelle e file, compongo questo menu.
    la funzione è la seguente:
    Codice PHP:
    $path="../DOC/";

    $out =  dirExplorertrim($path) , "" 200 );
    echo 
    $out."\n";

    function 
    dirExplorer$path $str $step $stepLimit )
    {
    // SPACES calculates spaces
        
    $s "" ;
        
        for(
    $i=$i<$step ;$i++)
            
    $s.= "\t" ;
        
            
    // CASI BASE
            
    if( !file_exists($path) )
            {
                return 
    "";
            }
                     

                    
    $c strrpos($path"/"); 
            
    $d substr($path$c+1);
                    if( !
    is_dir($path) ){
                             return 
    "[*]<a href=\"".$path."\" onclick=\"window.open(this.href);return false;\">".strtoupper($d)."</a> - 
                    <a id='delFile"
    .$i."' href=\"javascript:void(0)\" title='".$d."'>rimuovi</a>\n" ;
                    
            }
            if( 
    $step $stepLimit )
            {
                return 
    $s."[*][X]Recursion limit reached - ".$path."\n" ;
            }
        
            
    // RICORSIONE PRINCIPALE
            //$str1 = $s."<ul>\n".$s."[*]".$path."\n" ;
        
            // scorre le sotto dir
            
    $subs  scandir$path ) ;
            
            
            if(
    $path!="../DOC/"){
                               
    $str1 "[*]<a href=\"javascript:void(0)\">".$d."</a> - <a id='delFile".$i."' href=\"javascript:void(0)\" title='".$d."'>rimuovi3</a>\n";
                    }
            else{
                               
    $str1 "\n";
            }
        
            
    $str1 .= "<div class=\"tree-menu\">";    
            
    $str1 .= "[list=1]" ;
            foreach( 
    $subs as $n=>$v )
            {
                
    // Filtra il caricamento dei file "." e ".."
                
    if( $v!="" && $v!="." && $v!="..")
                {
                    
    // FORMATTAZIONE PERCORSO
                    
    $npath $path."/".$v ;
                    
    //echo $npath . "\n
    ";
                    if( 
    $path[ strlen($path)-1 ] == "/" )
                        
    $npath = $path.$v ;
                                     // se vuoi organizzare files e directories devi formattare
                    // degli array che scorrerai sotto spostando la chiamata ricorsiva
                    // scorrendo tali arrays cosi' puoi gestire prima le dir e poi i files
                    // echo "
    \n
    Npath
    " . $npath . " STR1" . $str1 . " Step" . ++$step . " StepLimit" . $stepLimit
                    
    $str1 .= "".dirExplorer( $npath , $str1 , ++$step , $stepLimit )."" ;
                }
            }
            
    $str1 .= "[/list]";
            
    $str1 .= "</div>";
            
    $str1 .= $s."\n".$s."\n

    " ;
            
            return 
    $str1;
        

    Praticamente devo assegnare ai link rimuovi un id univoco...
    Come faccio?
    grazie mille...
    Aiutatemi per favore...

  2. #2
    potresti aggiungere un parametro alla funzione dirExplorer

    Codice PHP:
    function dirExplorer$path $str $step $stepLimit $index
    passato dal chiamante esterno con valore 0

    Codice PHP:
    $out =  dirExplorertrim($path) , "" 200); 
    che viene incrementato ogni volta che trovi un file
    Codice PHP:
    if( !is_dir($path) )

       return 
    "[*]<a href=\"".$path."\" onclick=\"window.open(this.href);return false;\">".strtoupper($d)."</a> - 
       <a id='delFile" 
    $index "' href=\"java-script:void(0)\" title='".$d."'>rimuovi</a>\n" 

       
    $index ++;           

    e viene ripassato alla chiamata ricorsiva

    Codice PHP:
    $str1 .= "".dirExplorer$npath $str1 , ++$step $stepLimit $index)."" 
    Saluti.

  3. #3
    ti ringrazio della risposta, ma purtroppo mi restituisce il numero zero '0' su tutti gli id.
    Cosa devo fare?
    grazie ancora

  4. #4
    In effetti ho guardato distrattamente il codice e non mi sono accorto che la chiamata ricorsiva è dentro un for :P
    La soluzione potrebbe essere quella di dichiarare una variabile globale all'inizio del codice

    Codice PHP:
       global $index;

       
    $index 0
    e passarla alla prima chiamata della funzione dirExplorer

    Codice PHP:
    $out =  dirExplorertrim($path) , "" 200$index ); 
    Ovviamente rimangono invariate le modifiche del mio post precedente:

    Codice PHP:
    if( !is_dir($path) ) 

       return 
    "[*]<a href=\"".$path."\" onclick=\"window.open(this.href);return false;\">".strtoupper($d)."</a> - 
       <a id='delFile" 
    $index "' href=\"java-script:void(0)\" title='".$d."'>rimuovi</a>\n" 

       
    $index ++;            

    e

    Codice PHP:
       $str1 .= "".dirExplorer$npath $str1 , ++$step $stepLimit $index)."" 
    Saluti.

  5. #5
    niente da fare.
    Purtroppo ad $index mi assegna sempre zero '0'.

  6. #6
    ti posto il codice corretto:

    Codice PHP:
    $path="../DOC/"

    $index 0;

    $out =  dirExplorertrim($path) , "" 200 ); 
    echo 
    $out."\n"

    function 
    dirExplorer$path $str $step $stepLimit 

        global 
    $index;
    // SPACES calculates spaces 
        
    $s "" 
         
        for(
    $i=$i<$step ;$i++) 
            
    $s.= "\t" 
         
            
    // CASI BASE 
            
    if( !file_exists($path) ) 
            { 
                return 
    ""
            } 
                      

                    
    $c strrpos($path"/"); 
            
    $d substr($path$c+1); 
                    if( !
    is_dir($path) ){ 
                             return 
    "[*]<a href=\"".$path."\" onclick=\"window.open(this.href);return false;\">".strtoupper($d)."</a> - 
                    <a id='delFile"
    .$index ++."' href=\"java-script:void(0)\" title='".$d."'>rimuovi</a>\n" 
                     
            } 
            if( 
    $step $stepLimit 
            { 
                return 
    $s."[*][X]Recursion limit reached - ".$path."\n" 
            } 
         
            
    // RICORSIONE PRINCIPALE 
            //$str1 = $s."<ul>\n".$s."[*]".$path."\n" ; 
         
            // scorre le sotto dir 
            
    $subs  scandir$path ) ; 
             
             
            if(
    $path!="../DOC/"){ 
                               
    $str1 "[*]<a href=\"java-script:void(0)\">".$d."</a> - <a id='delFile".$index ++."' href=\"java-script:void(0)\" title='".$d."'>rimuovi3</a>\n"
                    } 
            else{ 
                               
    $str1 "\n"
            } 
         
            
    $str1 .= "<div class=\"tree-menu\">";     
            
    $str1 .= "[list=1]" 
            foreach( 
    $subs as $n=>$v 
            { 
                
    // Filtra il caricamento dei file "." e ".." 
                
    if( $v!="" && $v!="." && $v!=".."
                { 
                    
    // FORMATTAZIONE PERCORSO 
                    
    $npath $path."/".$v 
                    
    //echo $npath . "\n
    "; 
                    if( 
    $path[ strlen($path)-1 ] == "/" ) 
                        
    $npath = $path.$v ; 
                                     // se vuoi organizzare files e directories devi formattare 
                    // degli array che scorrerai sotto spostando la chiamata ricorsiva 
                    // scorrendo tali arrays cosi' puoi gestire prima le dir e poi i files 
                    // echo "
    \n
    Npath
    " . $npath . " STR1" . $str1 . " Step" . ++$step . " StepLimit" . $stepLimit
                    
    $str1 .= "".dirExplorer( $npath , $str1 , ++$step , $stepLimit )."" ; 
                } 
            } 
            
    $str1 .= "[/list]"; 
            
    $str1 .= "</div>"; 
            
    $str1 .= $s."\n".$s."\n

    " ; 
             
            return 
    $str1
         


Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.