Page 1 of 1

Listing files in numeric order....

Posted: Sat Aug 11, 2007 11:44 am
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?

Posted: Sat Aug 11, 2007 11:54 am
by Benjamin
Please edit your post so that your code is in PHP tags.

Posted: Sat Aug 11, 2007 12:43 pm
by superdezign
Can't you load all of the file names into an array, and then sort the array...?

natsort()

Posted: Sat Aug 11, 2007 8:01 pm
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!

Posted: Sat Aug 11, 2007 8:29 pm
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.

Posted: Sat Aug 11, 2007 9:24 pm
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.

Posted: Mon Aug 13, 2007 3:06 pm
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!

Posted: Mon Aug 13, 2007 3:32 pm
by John Cartwright
foreach()

Posted: Mon Aug 13, 2007 6:30 pm
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.

Posted: Mon Aug 13, 2007 8:18 pm
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. ^^^