Listing files in numeric order....

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
jkashu
Forum Commoner
Posts: 45
Joined: Tue Jan 30, 2007 12:00 pm

Listing files in numeric order....

Post by jkashu »

I need to list the files in a directory in numeric order. All the files are named 1, 2, 3, etc. with no file extensions.

I have a script that lists the files, but it seems to order then randomly...I need them to be in order.

Here is the script:

Code: Select all

<?php

function list_files($dir)
{
  if(is_dir($dir))
  {
    if($handle = opendir($dir))
    {
      while(($file = readdir($handle)) !== false)
      {
        if($file != "." && $file != ".." && $file != "Thumbs.db"/*pesky windows, images..*/)
        {
          echo '<a href="gallery1_page.php?page='.$file.'.php" target="_blank">'.$file.'</a>, '."\n";
        }
      }
      closedir($handle);
    }
  }
}
?>
<font size="3">
<?

list_files("pages/");

?>
Specifically, this script lists the files and makes the each number a link to another page.

Any ideas on the ordering?
Last edited by jkashu on Sat Aug 11, 2007 7:58 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Please edit your post so that your code is in PHP tags.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Can't you load all of the file names into an array, and then sort the array...?

natsort()
jkashu
Forum Commoner
Posts: 45
Joined: Tue Jan 30, 2007 12:00 pm

Post by jkashu »

Ok, how would i modify the current script to make an array with the filenames? ...so that I can use the sort function you mentioned...
thanks!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

jkashu wrote:Ok, how would i modify the current script to make an array with the filenames? ...so that I can use the sort function you mentioned...
thanks!
Instead of echoing... put it into an array. Then sort it after the loop. THEN echo the data.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

There are numerous ways to add elements to an array. In your case you'll simply take an empty array and add elements to it. To do this you would do something like..

Code: Select all

$arrayFileList = array();

while (yougetfiles)
{
    $arrayFileList[] = $file;
}
Now you have a container filled with all your filenames and you can do things to it.

See http://www.php.net/manual/en/ref.array.php for a list of array functions if you haven't already.
jkashu
Forum Commoner
Posts: 45
Joined: Tue Jan 30, 2007 12:00 pm

Post by jkashu »

OK I am able to create the array and use natsort() as well, but how do I display it?

I need it to list the files like this:

1,2,3,4

....with each number being a link to 1.php , 2.php, etc.

Thanks!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

foreach()
jkashu
Forum Commoner
Posts: 45
Joined: Tue Jan 30, 2007 12:00 pm

Post by jkashu »

Duh, foreach() makes sense....however, it still doesn't work. The out put is:

3,1,3,1,3,4,1,2,3,4,

It should be 1,2,3,4

The code is:


Code: Select all

<?php
			  
$arrayFileList = array(); 

function list_files($dir)
{
  if(is_dir($dir))
  {
    if($handle = opendir($dir))
    {
      while(($file = readdir($handle)) !== false)
      {
        if($file != "." && $file != ".." && $file != "Thumbs.db"/*pesky windows, images..*/)
        {
		$arrayFileList[] = $file;
		natsort($arrayFileList);
		foreach ($arrayFileList as $value){
          echo "<a href=\"gallery1_page.php?page=$value.php\">".$value."</a>,";
		  }
        }
      }
      closedir($handle);
	  
    }
  }
}


?>
              <font size="3">
              <?

list_files("pages/");

?>
Note* It needs to link to gallery1_page.php?page= for each number.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

superdezign wrote:Instead of echoing... put it into an array. Then sort it after the loop. THEN echo the data.
Read it again. ^^^
Post Reply