View Single Post
Old 08-31-2010, 03:26 AM   PM User | #21
freemany
New to the CF scene

 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
freemany is an unknown quantity at this point
Thumbs up Php way to do the sorting

Quote:
Originally Posted by freemany View Post
Before sorting the listed files/directories alphabetically, I coverted the function into the way listing the files/directories in list(ul/li) manner instead of the current 'intent' one. I reckon the list way is the most proper way to list nested items. Here is the new function:

PHP Code:
function getDirectory$path '.'){      
    
    
$ignore = array( 'cgi-bin''.''..' );
    
// Directories to ignore when listing output. Many hosts
    // will deny PHP access to the cgi-bin.

    
$dh = @opendir$path );
    
// Open the directory to the handle $dh
    
    
while( false !== ( $file readdir$dh ) ) ){
    
// Loop through the directory
        
        
if( !in_array$file$ignore ) ){
        
// Check that this file is not to be ignored
            
            // Just to add spacing to the list, to better
            // show the directory tree.
            
            
if( is_dir"$path/$file" ) ){
            
// Its a directory, so we need to keep reading down...
            
                               
                //Add a class as selector for the jQuery sorting later.
                
echo "<li>$file<ul class='has-children'>"
                
getDirectory"$path/$file");
                
// Re-call this same function but on a new directory.
                // this is what makes function recursive.
                
echo "</li>";
            
            } else {
            
                echo 
"<li>$file</li>";
                
// Just print out the filename
            
            
}
            
            
        
        }
        
    
    
    }
    echo 
"</ul>";
    
closedir$dh );
    
// Close the directory handle

}
//Add a class as selector for the jQuery sorting later.
echo "<ul class='has-children'>";
getDirectory"." ); 
Then add the following jQuery codes to sort the lists:

Code:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function() {   
$('ul.has-children').each(function() {
	var $t=$(this);
	var Li=$t.find('>li').get();
	Li.sort(function(a, b) {
             var A = $(a).text().toUpperCase();
             var B = $(b).text().toUpperCase();
             if (A < B) return -1;    // changing to 1 to sort in DESC order...
             if (A > B) return 1;     // changing to -1 to sort in DESC order...
           return 0;
        });
	 $.each(Li,function(i,v) {
	      $t.append(v);
	  });
});
</script>
Have fun
Here is the PHP way to do the same sorting as the above with jQuery:
PHP Code:
<?
$tree
=array();

function 
getDirectory$path '.'){      
    
    
$ignore = array( 'cgi-bin''.''..' );
    
// Directories to ignore when listing output. Many hosts
    // will deny PHP access to the cgi-bin.

    
$dh = @opendir$path );
    
// Open the directory to the handle $dh
    
    //Make counter
    
$j=0;
    
$temp=array();
        
    while( 
false !== ( $file readdir$dh ) ) ){
    
// Loop through the directory
        
        
if( !in_array$file$ignore ) ){
        
// Check that this file is not to be ignored
            
            // Feed with file name           
            
$temp[$j]['name']=$file;
            
            if( 
is_dir"$path/$file" ) ){
            
// Its a directory, so we need to keep reading down...    
               
$temp[$j]['children']=getDirectory"$path/$file");
            } 
       
        }
        
       
$j++; //counting
    
    
}
    return 
$temp;
   
    
closedir$dh );
    
// Close the directory handle

}//end of function

//Put the file directory system in an array first...
$tree=getDirectory("/your-dirtory/here");

$type='desc'//set sorting type 'desc' or 'asc'


//recursive function for sorting arrays
function getSort(&$temp) {
 global 
$type;

 switch (
$type) {
      case 
'desc':
      
rsort($temp);
      break;
      case 
'asc':
      
sort($temp);
      break;
}
     
 foreach(
$temp as &$t) {
   if(
is_array($t['children']))
                       
getSort($t['children']);
 }
//end of function

//Go through arrays again for sorting...now we have new sorted array $tree...     
getSort($tree);
?>
<pre>
<?
print_r
($tree);  // output 
?>
</pre>
Have funs
freemany is offline   Reply With Quote