Jump to content

need help sorting alphabetically and numerically


lm_a_dope

Recommended Posts

hello i have this script that outputs all directory's and files the problem i am having is i can sort alphabetically

 

say i have a file named

 

a1

a2

a10

a3

 

the way it is sorting them is like this

a1

a10

a2

a3

 

but i want to sort them like this

a1

a2

a3

a10

 

 

here is my script please help its driving me nutty

 

 

 

<?php

function Treemenu($path)

{

    //using the opendir function

    $dir_handle = @opendir($path) or die("Unable to open $path");

   

    //Leave only the lastest folder name

    $dirname = end(explode("/", $path));

$TEST = array();   

$i=0;

    //display the target folder.

    echo ("<li>$dirname\n");

    echo "<ul>\n";

    while (false !== ($file = readdir($dir_handle)))

    {

        if($file!="." && $file!="..")

        {

            if (is_dir($path."/".$file))

            {

                //Display a list of sub folders.

                Treemenu($path."/".$file);

            }

            else

            {

                //Display a list of files.

                $TEST[$i] = "<li><a href=\"{$path}/{$file}\">" .str_replace('.htm','',$file). "</a></li>";

$i++;

            }

        }

    }

sort($TEST);

echo implode('', $TEST);

    echo "</ul>\n";

    echo "</li>\n";

   

    //closing the directory

    closedir($dir_handle);

}

Treemenu('games')

?>

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.