Forum Moderators: coopster

Message Too Old, No Replies

sorting specific files

need help with a sorting issue

         

epsd

2:02 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



I have an array of files, let's say: a.html b.html c.html and home.html.

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!

PHP_Chimp

2:20 pm on Dec 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$files_length = count($files);
for ($i=0; $i<$files_length; $i++) {
if ($files[$i]!= 'home.html') {
// do whatever
}
else {
// do whatever with home.html
}

Is there any reason why you are using home.html as opposed to just '/'? Not that it makes any difference to this script, just why not use the default index page instead of renaming it home.

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

epsd

2:43 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



home.html is really named index.html, just wanted to be clear in explaining the problem.

epsd

3:19 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



Do I still need to do the foreach inside the if (see below)?

$files_length = count($files);
for ($i=0; $i<$files_length; $i++) {
if ($files[$i]!= 'home.html') {
foreach ($files as $file) {
// do whatever
}
}
else {
foreach ($files as $file) {
// do whatever with home.html
}
}

PHP_Chimp

5:49 pm on Dec 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No as the for loop will go through all of the values in the list, so there is no point in having a foreach loop under there. As the input for that block of code is a scalar not an array, so foreach wont do anything.

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]

epsd

7:06 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



Not sure what i'm missing but it still lists the files in alpha order (without index.php being at the top). Here is the code:

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
}
}

gergoe

7:14 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



ksort() makes no sense in this case, because that sorts the array by the keys, and probably your array is filled by a function like scandir(), which already puts the files/directories in alphabetic order. To really make the array sorted alphabetically but keep the home.html on top of the list, you need to use usort() [php.net].

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");

epsd

7:19 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



You nailed it gergoe! Fantastic job and many thanks!