Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to sort display of files in a directory
Old 12-07-2007, 05:46 PM How to sort display of files in a directory
Novice Talker

Posts: 6
Name: Joe Johnson
Trades: 0
I have a simple PHP page that lists files in a directory:
PHP Code:
<?php
$dir 
"./FORMS/SNF/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
   if (
$dh opendir($dir)) {
       while ((
$file readdir($dh)) !== false) {
    if(
$file == "." || $file == ".." )
        continue;
           echo 
"<a href=\"".$dir.$file."\">".$file."</a><br/>";
       }
       
closedir($dh);
   }
}
?>
I'm having trouble sorting the display. I think I would use
PHP Code:
sort($file
but where do I put it? I tried putting it right under the 'while (($file...' line but that causes an error "Parse error: parse error, unexpected T_IF in /usr/local/php_classes/forms/PHP4_scandir3.php on line 26." When I put it in after 'continue;' I get the list, but with this warning: "Warning: sort() expects parameter 1 to be array, string given in /usr/local/php_classes/forms/PHP4_scandir3.php on line 27."

I'm sure I'm making this harder than it needs to be. Thanks in Advance.
jej1216 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-08-2007, 06:14 AM Re: How to sort display of files in a directory
nabil_kadimi's Avatar
Experienced Talker

Posts: 42
Name: nabil
Trades: 0
Well... the answer is in the error message you get :
Warning: sort() expects parameter 1 to be array

Instead of displaying the files within the while loop, you can store filenames on an array:
replace
PHP Code:
echo "<a href=\"".$dir.$file."\">".$file."</a><br/>"
with
PHP Code:
$files[]=$file
Then after the while loop you call the sort() function and then start another while (for/foreach) loop to display the sorted array
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
nabil_kadimi is offline
Reply With Quote
View Public Profile
 
Old 12-10-2007, 12:17 PM Re: How to sort display of files in a directory
Novice Talker

Posts: 6
Name: Joe Johnson
Trades: 0
Thanks -

I no longer get the error, but the results are not sorting. Here is what I have changed in the code:
PHP Code:
$dir "./FORMS/SNF/";
if (
is_dir($dir)) {
   if (
$dh opendir($dir)) {
       while ((
$file readdir($dh)) !== false) {
       if(
$file == "." || $file == ".." )
        continue;
        
$files[]=$file;
       }
        
sort($file);
       foreach (
$file as $key=>$value) {
       echo 
"<a href=\"".$dir.$file."\">".$file."</a><br/>";
       }
       
closedir($dh);
   }

The results are not sorted (the file names all start with a number "010 Attendance","101 Incident", etc.

The mistake is in my foreach loop, right?

Thanks,
jej1216 is offline
Reply With Quote
View Public Profile
 
Old 12-10-2007, 01:41 PM Re: How to sort display of files in a directory
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
No.
Your problem is that the sort function sort by the first characters in the string. In your case, 010, 101, etc...

The sorting is right upon that criteria.
Sort have 1 parameter that you can give to alter it's algorithm, but from what I see, you will need to sort not on the full file name, but on a substring of the file name, ignoring the first numeric part.
Am I right ?

If so, you will need to create an "reference" array, and the real files array.
The reference will hold the shortened name only, and the real files array which will not be numerical but indexed by strings.

What I mean is that you need to create it like this:
PHP Code:
$shortName=myShorteningFunction($fullName);
$aryFiles[$shortName]=$fullName
This will create an array, but it will index it via a string, and not a number.

You order the reference array, and after it's sorted, you iterate through it.
For each short name, you call the corresponding value in the longName array.
PHP Code:
sort($aryRef);
foreach(
$aryRef as $shortName){
  echo 
$aryFiles[$shortName];

__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-10-2007, 02:12 PM Re: How to sort display of files in a directory
Novice Talker

Posts: 6
Name: Joe Johnson
Trades: 0
Thanks - actually, I want this type of sort:

If the listed files are:
110 Restraint Flowsheet 04_07 - SNF.pdf
137b Physical Therapy Progress Notes - SNF.pdf
150 Activity Assessment 04_07 - SNF.pdf
101 Daily Nursing Assessment 04_07 - SNF.pdf
209-210 MOT Consent Refusal 04_07 - SNF.pdf

I want them to display as:
101 Daily Nursing Assessment 04_07 - SNF.pdf
110 Restraint Flowsheet 04_07 - SNF.pdf
137b Physical Therapy Progress Notes - SNF.pdf
150 Activity Assessment 04_07 - SNF.pdf
209-210 MOT Consent Refusal 04_07 - SNF.pdf

I really just need to sort on the first three characters, including the numbers. Is it the same approach you describe? Will a string sort put the "0" before the "1" asnd the "1" before the "2" etc?

As you might have deduced, I'm a PHP Newbie.

Thanks,
jej1216 is offline
Reply With Quote
View Public Profile
 
Old 12-10-2007, 03:04 PM Re: How to sort display of files in a directory
Novice Talker

Posts: 6
Name: Joe Johnson
Trades: 0
I mispoke.
PHP Code:
$dir "./FORMS/SNF/";
if (
is_dir($dir)) {
   if (
$dh opendir($dir)) {
       while ((
$file readdir($dh)) !== false) {
       if(
$file == "." || $file == ".." )
        continue;
        
$files[]=$file;
       }
        
sort($files);
       foreach (
$files as $key=>$value) {
       echo 
"<a href=\"".$dir.$file."\">".$file."</a><br/>";
       }
       
closedir($dh);
   }

echos nothing at all.

I tried
PHP Code:
$dir "./FORMS/SNF/";
if (
is_dir($dir)) {
   if (
$dh opendir($dir)) {
       while ((
$file readdir($dh)) !== false) {
       if(
$file == "." || $file == ".." )
        continue;
        
$files[]=$file;
        
sort($files);
        echo 
"<a href=\"".$dir.$file."\">".$file."</a><br/>";
                                                }
                            }
       
closedir($dh);
                  } 
and get the list, but unsorted.
jej1216 is offline
Reply With Quote
View Public Profile
 
Old 12-10-2007, 03:27 PM Re: How to sort display of files in a directory
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
PHP Code:
$dir "./FORMS/SNF/";

function 
shortimg($str){
  list(
$short)=explode(" ",$str,2);
  return 
$short;
}

if (
is_dir($dir)) {
  if (
$dh opendir($dir)) {
    while ((
$file readdir($dh)) !== false) {
      if(
$file !== "." && $file !== ".." )
        
//We get the short version of the file name
        //ans store them in 2 arrays
        
$short=shortimg($file);
        
$aryRef[]=$short;
        
$files[$short]=$file;
      }
      
//We force PHP to oerder them using a numeric algorithm
      
sort($aryRef,SORT_NUMERIC);
      
//And we parse the reference array
      
foreach ($aryRef as $key) {
        
//Here, $key is the same as that was $short previously
        
$file=$files[$key];
        
//1) You don't need to escape variables in strings if the strings delimiter are "
        //2) In a web environment, the directory separator is shlash ( / ), and not backslash ( \ ) like in windows/dos shell
        
echo "<a href=\"$dir/$file\">$file</a><br/>";
      }
      
closedir($dh);
  }

I didn't tried to run it, but this should do the trick...
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 12-10-2007 at 03:29 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-10-2007, 04:45 PM Re: How to sort display of files in a directory
Novice Talker

Posts: 6
Name: Joe Johnson
Trades: 0
tripy,

Thanks!

It sorts like I need.

It is listing ".." twice and "." twice, but I'll work on that part.

Thanks again for your help.
jej1216 is offline
Reply With Quote
View Public Profile
 
Old 12-10-2007, 07:28 PM Re: How to sort display of files in a directory
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
It is listing ".." twice and "." twice, but I'll work on that part.
I think I missed a {} block...
Try like this:
PHP Code:
$dir "./FORMS/SNF/";

function 
shortimg($str){
  list(
$short)=explode(" ",$str,2);
  return 
$short;
}

if (
is_dir($dir)) {
  if (
$dh opendir($dir)) {
    while ((
$file readdir($dh)) !== false) {
      if(
$file !== "." && $file !== ".." ){
        
//We get the short version of the file name
        //ans store them in 2 arrays
        
$short=shortimg($file);
        
$aryRef[]=$short;
        
$files[$short]=$file;
      }
    }
    
//We force PHP to oerder them using a numeric algorithm
    
sort($aryRef,SORT_NUMERIC);
    
//And we parse the reference array
    
foreach ($aryRef as $key) {
      
//Here, $key is the same as that was $short previously
      
$file=$files[$key];
      
//1) You don't need to escape variables in strings if the strings delimiter are "
      //2) In a web environment, the directory separator is shlash ( / ), and not backslash ( \ ) like in windows/dos shell
      
echo "<a href=\"$dir/$file\">$file</a><br/>";
    }
    
closedir($dh);
  }

</span></span>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to How to sort display of files in a directory
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46152 seconds with 12 queries