Jump to content


Photo

Change loop order


  • Please log in to reply
3 replies to this topic

#1 djones

djones
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts
  • LocationWinston-Salem, NC

Posted 31 March 2006 - 03:35 PM

In my directory listing, it lists files in alphabetical order by default. How can I change the ordering?


    for ($i=0; $i<=sizeof($files)-1; $i++) {
        $p1 = strrpos($files[$i],"/") + 1;
        $p2 = strpos($files[$i],".flv");
        $name = substr($files[$i],$p1,($p2-$p1));
        echo "        <li><a href=\"chriswalker.php?file=".($i+1)."\">".str_replace("%20"," ","$name")."</a></li>\n";
    }


#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 31 March 2006 - 05:13 PM

I don't know about a server option that might do it. But if you're talking about reading each file and displaying it yourself, then you can read all the files/directories (opendir() & readdir(), or scandir() for PHP5) into an array then sort the array in reverse order using rsort(). Then iterate through the sorted array and display each file/directory.

[a href=\"http://us3.php.net/manual/en/function.rsort.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.rsort.php[/a]

[a href=\"http://us3.php.net/manual/en/function.opendir.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.opendir.php[/a]

[a href=\"http://us3.php.net/manual/en/function.readdir.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.readdir.php[/a]

[a href=\"http://us3.php.net/manual/en/function.scandir.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.scandir.php[/a]

#3 djones

djones
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts
  • LocationWinston-Salem, NC

Posted 31 March 2006 - 06:19 PM

Unfortunately we don't have PHP5 installed yet. We are doing a server upgrade in May that will include PHP5 MySQL 5 and Apache 2.2.

So there's no way to change the default ordering that php lists files in a directory? I need to list the files in DESC order and not ASC. kinda like you would do in SQL.


#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 31 March 2006 - 06:22 PM

I guess you didn't look at the links provided. I mentioned that only scandir() is a PHP 5 function. The rest aren't.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users