Originally posted by FlipinMonkeyPie
i want to know how to make the $dirlisting alphabatized, what do i need to change, how can i make it an array and still have all the info in it..
What's probably easiest is to make it an array with all the info in it, then alphabetise it.
Change
$dirlisting .= "<li><a href="$PHP_SELF?a=dir&dir=1\" class=\"top_menu_2\">$dir_name</a> ($numlinks)</li>";
to
$dirlisting[] = "<li><a href=\"$PHP_SELF?a=dir&dir=1\" class=\"top_menu_2\">$dir_name</a> ($numlinks)</li>";
And after the loop:
sort($dirlisting); // To sort
$dirlisting=join('',$dirlisting); // To turn it into a single string.
That will sort the way you want, but more through luck than anything else.
Better would be
$dir['dir_name']=$dir_name;
$dir['numlinks']=$numlinks;
$dirlisting[]=$dir;
in the loop. Then afterwards:
} // This is the end of the while loop.
function compare_dirname($a,$b)
{ return strcmp($a['dir_name'], $b['dir_name']); // This is the way we want them sorted.
}
usort($dirlisting, 'compare_dirname'); // So sort them that way.
foreach($dirlisting as $k=>$dir)
{ $dirlisting[$k] = "<li>...$dir[dir_name]...$dir[numlinks]...</li>"; // Apply the formatting
}
$dirlisting=join('', $dirlisting);
That's off the top of my head. The fact I keep reusing $dirlising implies a guess that you'd have no further use for the contents of the array after this string has been bult up.
But the general principle is probably more important than the specific implementation. Structure your data right in the first place, and the code will follow. Good programming practice, that.