SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: sort problem
-
Jul 4, 2007, 13:18 #1
sort problem
I'm having a hell of a time sorting this alphabetically. Could somebody help?
PHP Code:$currentdir = SITE_PATH.$directory;
$new_file = null;
@$dir = opendir($currentdir);
while($file = readdir($dir)) {
if(!is_dir($currentdir."/".$file)) {
$file = $file . "','";
$new_file .= $file;
}
}
-
Jul 4, 2007, 13:52 #2
- Join Date
- Mar 2007
- Location
- Toronto
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Are you just trying to sort the files in a directory alphabetically?
Stick them inside an array, then use asort()
PHP Code:@$dir = opendir($currentdir);
while($file = readdir($dir))
if(!is_dir($currentdir."/".$file))
$files[] = $file;
asort($files);
-
Jul 4, 2007, 13:54 #3
- Join Date
- Aug 2005
- Posts
- 453
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Instead of a flat file, any thoughts of creating an array of arrays ? You can then iterate through and use asort().
Computers and Fire ...
In the hands of the inexperienced or uneducated,
the results can be disastrous.
While the professional can tame, master even conquer.
-
Jul 4, 2007, 14:18 #4
Thank you.
The problem that I have is that the $new_file var is put into a pulldown, which then displays filenames of images in the dir.
I can't really toy with the code, and what you gave me doesn't work too well. Meaning, instead of each image being an <option>, all of the images in the dir showed up as one <option>. Is there a way to sort with the code that I supplied?
-
Jul 4, 2007, 14:55 #5
- Join Date
- Mar 2007
- Location
- Toronto
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you want to get all the files inside $new_file spaced with a comma, just use implode
PHP Code:$new_file = implode(",", $files);
//now $new_file = 'file1,file2,file3...'
PHP Code:foreach($files as $file)
$dropdown .= "<option>$file</option>";
Bookmarks