Page 1 of 1

Alphabetical Order

Posted: Wed Sep 24, 2008 7:09 pm
by enemeth
Hi there,

need some help here! got this code

Code: Select all

 
<?php  
$path = "My Path";  
$browserpath = "/uploads";  
$files = "<ul>\n";   
$dir = opendir($path);  
 
while($file = readdir($dir)) {  
  if(($file != ".") && ($file != "..")) {  
    $files .= "\t<li><a href=\"$browserpath/$file\">$file</a></li>\n"; 
  
  }   
}  
closedir($dir);  
 
?>  
<font face="Verdana" size="4">Files in <?=$browserdir?>:  
<?=$files 
?>  
</font> 
 
 
what it does you all probably know, it just displays the files that are in the path above on the web page.... but what i need it to do is just the same but in alphabetical order, can someone help me ?

Thx,
Elaine

Re: Alphabetical Order

Posted: Wed Sep 24, 2008 7:41 pm
by Christopher

Re: Alphabetical Order

Posted: Wed Sep 24, 2008 9:26 pm
by enemeth
didnt help, i guess i am not seeing it, anyone out there that can just jot down a line of code that will display the list in alphabetical order?

thx,
Elaine

Re: Alphabetical Order

Posted: Thu Sep 25, 2008 12:10 am
by Christopher
Did you look at Example #1 on that page?

Re: Alphabetical Order

Posted: Thu Sep 25, 2008 6:59 am
by enemeth
yes i did i replaced it , but i think im doing something wrong, i dont no php that well i tried to implement it but it didnt work, can you point me in the right direction on what i need to put in my code from that to make it work?

Re: Alphabetical Order

Posted: Thu Sep 25, 2008 7:17 am
by Darkzaelus
Why don't you put the filenames in a array instead of attaching them to a string, then use array_sort() on them. Then you can loop through them when it comes to outputting them?

Cheers,

Darkzaelus.

Re: Alphabetical Order

Posted: Thu Sep 25, 2008 7:32 am
by enemeth
well that sounds good, but i dont no how to do that lol

;)

Re: Alphabetical Order

Posted: Thu Sep 25, 2008 7:37 am
by Darkzaelus

Code: Select all

 
$files = array();  
$dir = opendir($path);  
$i=0;
while($file = readdir($dir)) {  
  if(($file != ".") && ($file != "..")) {  
    $files[$i]=$file;
    $i++;
  }   
}  
$files=sort($files,SORT_STRING);
$b=sizeof($files);
for($i=0;$i<$b;$i++)
    echo $files[$i]; //Adjust to do what you want. At the moment it will print out the filenames in order.
 
Cheers,

Darkzaelus.

Re: Alphabetical Order

Posted: Thu Sep 25, 2008 7:40 am
by VladSun
++arborint
glob() will always sort filenames, unless you specify the
GLOB_NOSORT - Return files as they appear in the directory (no sorting)
flag ...