...

View Full Version : Sorting alphabetically



spok61438
02-26-2008, 07:42 AM
<?
$dir = "music/";
unset($param);
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file != ".." && $file != "." && strstr($file,"mp3")) {
if ($param == "") {
$param = "music/" . rawurlencode($file);
} else {
$param .= "|" . "music/" . rawurlencode($file);
}
}
}
closedir($dh);
}
}
?>

What should I add to the script to make it sort the files alphabetically?

Thanks in advance!

rafiki
02-26-2008, 10:14 AM
try http://php.net/sort

JimM
02-26-2008, 10:28 AM
Something like this asort(),
bad thing is upper case will come before lower (i believe) IE A, B, C, a, c, e



function GetDL($dir) {
$dl = array();
if ($hd = opendir($dir)) {
while ($sz = readdir($hd)) {
if (preg_match("/^\.\.?$/",$sz) || is_dir($dir.$sz)) { continue; }
$dl[] = $sz;
}
closedir($hd);
}
asort($dl);
return $dl;
}


Note: it is an array that is being sorted $dl

spok61438
02-26-2008, 04:43 PM
any one else?

matak
02-26-2008, 05:05 PM
dude, what's wrong with sort from #2? :confused:

spok61438
02-26-2008, 06:33 PM
<?php

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}

?>


what part should I include in array?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum