Alphabetical Order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
enemeth
Forum Commoner
Posts: 66
Joined: Tue Mar 27, 2007 8:55 am

Alphabetical Order

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Alphabetical Order

Post by Christopher »

(#10850)
enemeth
Forum Commoner
Posts: 66
Joined: Tue Mar 27, 2007 8:55 am

Re: Alphabetical Order

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Alphabetical Order

Post by Christopher »

Did you look at Example #1 on that page?
(#10850)
enemeth
Forum Commoner
Posts: 66
Joined: Tue Mar 27, 2007 8:55 am

Re: Alphabetical Order

Post 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?
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Alphabetical Order

Post 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.
enemeth
Forum Commoner
Posts: 66
Joined: Tue Mar 27, 2007 8:55 am

Re: Alphabetical Order

Post by enemeth »

well that sounds good, but i dont no how to do that lol

;)
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Alphabetical Order

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Alphabetical Order

Post 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 ...
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply