sort() problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
banpro
Forum Newbie
Posts: 13
Joined: Tue Aug 05, 2003 4:21 pm

sort() problems

Post by banpro »

Hi, I'm trying to use sort to alphabetize a listing of names but I'm having trouble with the functions. I need to open the list, read it into an array, sort it and then rewrite the list in alphabetic order. Here's what I have but it isn't working as expected.

Code: Select all

//Alpha sort the directory
     $fpp = fopen("listing.php", 'r');
     while(!feof($fpp))
        {
        $line = fgets($fpp, 1024);
        }
      fclose($fpp);
      sort($line);
      reset($line);
// Rewrite the directory      
      $ffpp = fopen("listing.php", 'w');
      fwrite($ffpp, "$line");
      fclose($ffpp);
Any ideas or suggestions are most welcomed and appreciated.

Thanks in advance,
Scott
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

not working as expected?

What is happening?
banpro
Forum Newbie
Posts: 13
Joined: Tue Aug 05, 2003 4:21 pm

Post by banpro »

It hangs on the sort() & resort() commands with the folowing errors:

Warning: sort() expects parameter 1 to be array

Warning: reset(): Passed variable is not an array or object

Thanks,
Scott
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

this line isn't creating an array

Code: Select all

$line = fgets($fpp, 1024);
it will just equal the last line of the file you are reading from.

Try this

Code: Select all

$x = 0;

while(!feof($fpp)) { 

        $line[$x] = fgets($fpp, 1024); 
        $x++
}
Mark
banpro
Forum Newbie
Posts: 13
Joined: Tue Aug 05, 2003 4:21 pm

Post by banpro »

Mark, thanks, that stops the errors. However, now the output back to the file after being sorted isn't the directory listing in alphabetic order, but rather just the word "Array"?

Here's the updated code now:

Code: Select all

//Alpha sort the directory
     $fpp = fopen("listing.php", 'r');
$x = 0; 
while(!feof($fpp)) { 
        $lineї$x] = fgets($fpp, 1024); 
        $x++;
} 
      fclose($fpp);
      sort($line);
      reset($line);

// Rewrite the directory      
      $ffpp = fopen("listing.php", 'w');
      fwrite($ffpp, "$line");
      fclose($ffpp);
Thanks again for the help,
Scott
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Try this mate

Code: Select all

//Alpha sort the directory 
     $fpp = fopen("listing.php", 'r'); 
$x = 0; 
while(!feof($fpp)) { 
        $line[$x] = fgets($fpp, 1024); 
        $x++; 
} 
      fclose($fpp); 
      sort($line); 
      reset($line); 

// Rewrite the directory      
      $ffpp = fopen("listing.php", 'w');
      foreach($line as $new_line) { // itterate through the array and write each line
           fwrite($ffpp, "$new_line\n"); // write the line and insert a line break
      }
      fclose($ffpp);
Mark
banpro
Forum Newbie
Posts: 13
Joined: Tue Aug 05, 2003 4:21 pm

Post by banpro »

Perfect! Mark, you're a life-saver.

I really need to re-RTM on arrays this evening.

Thanks for the help,
Scott
Post Reply