5
« on: May 25, 2008, 03:31:24 PM »
I've tried to change the get_dir function to sort alphabetically by last name, but I get error messages: Can anybody see anything wrong?
function get_dir($dir, $id, $pos = 0) {
global $a, $b, $file, $dir_array, $dir_parent_array, $id, $id2, $id3, $data_src_path, $thumbnail_options;
$handle = opendir($dir);
$folders = array();
while ($file = @readdir($handle)) {
if (@is_dir($dir.$file) && !eregi("^\.{1,2}$", $file) && $file != basename($thumbnail_options['subfolder'])) {
$folders[] = $file;
}
}
@closedir($handle);
$first_name = array();
$last_name = array();
$name_temp = array();
unset($file);
foreach ($folders as $file) {
$id2 = 0;
$id3 = 0;
if (substr($file,$id2,1) != ' ') { // This if-statement is supposed to separate
$first_name[$id2] = substr($file,$id2,1); // the first and last name.
$id2++;
}
else
{
$last_name[$id3] = substr($file,$id3,1); // This creates the last name
$id3++;
}
$name_temp = array( // Here it's supposed to create an array
array($first_name, $last_name) // of first_name and last_name
);
}
unset($file);
function compare_lastname($a, $b) // This function sorts the array by the last name.
{
return strnatcmp($a[1], $b[1]);
}
# sort alphabetically by name // This calls the above function.
usort($name_temp, 'compare_lastname');
$id = 0; // This puts the sorted folders back to one string (firstname
foreach ($name_temp as $file) { // and last name(, separated by a space.
$folders[$id] = $name_temp[$id][0] . ' ' . $name_temp[$id][1]; //Is this the right way to put it together?
$id++;
}
unset($file);
$id = 0;
foreach ($folders as $file) {
$id++;
$dir_array[$id] = array(
"cat_name" => $file,
"cat_path" => $dir.$file,
"cat_folder_path" => str_replace($data_src_path."/", "", $dir.$file),
"parent_id" => $pos
);
$dir_parent_array[$pos][] = $id;
get_dir($dir.$file."/", $id, $id);
}
return true;
}