Welcome Guest, Not a member yet? Register   Sign In
Sorting files alphabetically with function directory_map
#1

[eluser]Unknown[/eluser]
Hi my server filesystem does not sort files alphabetically - so my image list for my newsletter cms made in code igniter shows the images unordered. I've tried all sorts of sort code for the $filedata and nothing has worked - does anybody have any idea how to adjust this so my files are sorted properly? It's a big pain to have to scroll through the unordered list to select images!

I think the culprit is in the file /system/helpers/directory_helper.php

Code:
function directory_map($source_dir, $top_level_only = FALSE)
{    
    if ($fp = @opendir($source_dir))
    {
        $filedata = array();
              
            

        while (FALSE !== ($file = readdir($fp)))
        {
            if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.' AND $top_level_only == FALSE)
            {
                $temp_array = array();


                
                $temp_array = directory_map($source_dir.$file."/");
                
                $filedata[$file] = $temp_array;
                            


            }
            elseif (substr($file, 0, 1) != ".")
            {
                $filedata[] = $file;


            }
        }
        return $filedata;

            
    }
}
#2

[eluser]Unknown[/eluser]
ah my error here - this was an uber simple fix. I did switch to get_filenames instead of map_directory - probably not necessary - but the code to sort alphabetically is just this - in the systems/helpers/file_helper.php file

before
Code:
return $_filedata;

add
Code:
asort($_filedata);

So instead looks like this:

Code:
asort($_filedata);

return $_filedata;

Hope this can help somebody!
#3

[eluser]dsims[/eluser]
you could also use
Code:
array_multisort($filedata);
return $filedata;
works even if the array is not multi-dimensional




Theme © iAndrew 2016 - Forum software by © MyBB