Forum Moderators: coopster
In my script I do this with the array:
ksort($files); //$files are the list above
foreach ($files as $file) {
It prints them out in perfect alpha order. However, I would like it to first list home.html and then the rest in alpha order... I can't wrap my brain around where to start to get this to happen. In the sort or in the foreach? Any thoughts? Thanks as always!
$files_length = count($files);
for ($i=0; $i<$files_length; $i++) {
if ($files[$i]!= 'home.html') {
// do whatever
}
else {
// do whatever with home.html
}
If you have already built the site and called the default index page home then that can be sorted, so that you can still call the page '/'.
DirectoryIndex index.htm index.html home.html #add as many as you like
The else part should only be activated for the 'home.html' page, so again no foreach needed.
[edited by: PHP_Chimp at 5:50 pm (utc) on Dec. 10, 2007]
ksort($files); //changed to ksort for sorting issues from sort
//foreach ($files as $file) {
$files_length = count($files);
for ($i=0; $i<$files_length; $i++) {
if ($files[$i]!= 'index.php') {
// do whatever
echo "$files[$i]<br>";
}
else {
echo "$files[$i]<br>";
// do whatever with home.html
}
}
With this function you can implement your own sorting algorithm, which does an alphabetic comparison by default, but always considers home.html 'lower' than anything else, like this:
function cmp($a, $b) {
if ($a == 'home.html') return -1;
elseif ($b == 'home.html') return 1;
else return strcasecmp($a, $b);
}
usort($files, "cmp");