Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting list of files

Status
Not open for further replies.

blasterstudios

Technical User
Dec 30, 2004
128
US
I have a list of files showing up on a page that i scraped together from php.net. I need to sort these files alphabetically. The only thing i've read about sorting is that they have to be in an array, and I have trouble with arrays. Here's my code to get the files:
Code:
$startdir = $orgdir . "/files/";
   $handle = $startdir;
   if ($handle = opendir($startdir)) {
      while (false !== ($file = readdir($handle))) { 
      $filetype = explode(".", $file);
    if ($file != "." && $file != ".." && $file != "index.html" && $file != "index.php") {
echo "<a href=\"" . $startdir . $file; . "\" target=\"_blank\">" . $file . "</a>"; }
 
ok, like i said, i'm very new when it comes to arrays and am not familiar with how to do it. as i said in my post, i was aware the best way to do it is in an array, but i just don't know how.
 
Arrays are simple constructs. Think of them as lists. Each item in the list has a number or a position in the list.

to create an array, you would have to crate a variable to hold the array.

For your situation, we will name the variable
$file_array.

Now you want to be able to add items to the array, so the variable has to have an index. To do this justtake your variable $file_array, and add an index $file_array[0].

The number between the [] is the index, it definesthe positions of the items in the array.

So lets take your code.

You want to add each file it finds in the directory, to the array.

So you would add th following lines to your code:

Code:
...
 if ($file != "." && $file != ".." && $file != "index.html" 
&& $file != "index.php") {
[red]$file_array[$index]=$file;
$index++;[/red]
...

Since your code is alredy recurssing through the directory to print the files, all I did was add to lines to put them into the array, then before printing them you will need to sort the array to give them the order you want to have.


Using the [blue]sort()[/blue] function will arrange them alphabetically. And then you can just cycle through it to print them however you want.

going back to your code, it would be something like:

Code:
 if ($file != "." && $file != ".." && $file != "index.html" && $file != "index.php") {
$file_array[$inde]=$file;
$index++;
}
}[green]\\while[/green]
[green]when the while is done, sort the array[/green]
sort($file_array);
and print it.
for($i=0;$i<=$index;$i++)
{
echo "<a href=\"" . $startdir . $file_array[$i]; . "\" target=\"_blank\">" . $file_array[$i] . "</a>"; }
}

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top