Jump to content

Where does sort go?


Recommended Posts

Im using this script to display directories recursively, but im not smart enough to figure out how to modify it to sort the results alphabetically.  i know the function is sort() but i have no idea where to put it.  i tried:

sort(getDirectory( "./pricing/" )); but obviously that didn't work  ;D

 

<?php

function getDirectory( $path = '.', $level = 0 ){


    $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
            
            $spaces = str_repeat( ' ', ( $level * 4 ) );
            // 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...
            
                echo "<strong>$spaces $file</strong><br />";
                getDirectory( "$path/$file", ($level+1) );
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
            
            } else {
            
                echo "<a href=\"$path/$file\">$file</a><br />";
                // Print out the link to the file
            
            }
        
        }
    
    }
    
    closedir( $dh );
    // Close the directory handle

}

?>



  <?php
getDirectory( "./pricing/" );
?>

Link to comment
https://forums.phpfreaks.com/topic/114093-where-does-sort-go/
Share on other sites

You've got to build it a little different if you want sorting.

 

First, loop through all the files and store them in an array, sort the array, then loop through the array

 

<?php

$path = 'C:/wamp/www';

$ignore = array( 'cgi-bin', '.', '..' );

$dh = @opendir( $path );
    
$files = array();
while( false !== ( $file = readdir( $dh ) ) )
if( !in_array( $file, $ignore ) )
    	$files[] = $file;

sort( $files );

// Now loop through file array we've made
foreach ( $files as $file ) {
            
$spaces = str_repeat( ' ', ( $level * 4 ) );
           
if( is_dir( "$path/$file" ) ){
           
	echo "<strong>$spaces $file</strong><br />";
	#getDirectory( "$path/$file", ($level+1) );
           
} else {
           
	echo "<a href=\"$path/$file\">$file</a><br />";
           
}
       
}

?>

 

If you wanna sort by folders, then files, both alphabetically, just check to see if its a directory when you're building the array, and store directories in another array... sort them both, then just foreach the directory array and the files array separately.

Link to comment
https://forums.phpfreaks.com/topic/114093-where-does-sort-go/#findComment-586421
Share on other sites

Using the code you gave me in my function gives me a nice alphabetical list, but its no longer recursive.  I don't know what you mean when you say "sort directories in another array", or "just foreach the directory array".  I have no idea what I'm doing (obviously) and it seems I need to hire a professional.  :)

Link to comment
https://forums.phpfreaks.com/topic/114093-where-does-sort-go/#findComment-589708
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.