Jump to content

Sorting


jengels

Recommended Posts

Hello everyone. I've very new to php. :-[ I have some code here I'm trying to sort the list that it creates for me of the files in a directory. I'm not having much luck and hope someone could help me. Sorting by Alpha would be fine.

 


$ignore = array( 
  '.', '..', '_vti_cnf', 'index.php', 'InstructSheet.txt', 'Edit Instructions Sheet.php', 'upload.php', 'admin', 'uploadstandards.php'
); 
if ($handle = opendir('.')) 
{ 
  while (false !== ($file = readdir($handle))) 
  { 
     if (!in_array ($file, $ignore))
    { 

echo "\t<li>\n\t\t<a href=\"$file\">$file</a>\n<ul>$buffer</ul>\n\t</li>\n";
     } 
  }
  closedir($handle); 
} 

Link to comment
https://forums.phpfreaks.com/topic/191770-sorting/
Share on other sites

In order to sort the actual file names, you must first put them into an array.

 

You can use code similar to this recent thread - http://www.phpfreaks.com/forums/index.php/topic,287388.0.html (just remove the call to the array_reverse() function.)

Link to comment
https://forums.phpfreaks.com/topic/191770-sorting/#findComment-1010778
Share on other sites

Thank you for the replies. I think I'm still missing something. I've tried to setup an array for the files. I think I'm still missing something


$ignore = array( 
  '.', '..', '_vti_cnf', 'index.php', 'InstructSheet.txt', 'Edit Instructions Sheet.php', 'upload.php', 'admin', 'uploadstandards.php'
); 

$file = array();
sort ($file);

if ($handle = opendir('.')) 
{ 
  while (false !== ($file = readdir($handle))) 
  { 
  
     if (!in_array ($file, $ignore))
    { 

echo "\t<li>\n\t\t<a href=\"$file\">$file</a>\n<ul>$buffer</ul>\n\t</li>\n";

    } 
  }
  closedir($handle); 
} 

Link to comment
https://forums.phpfreaks.com/topic/191770-sorting/#findComment-1010818
Share on other sites

I looked a bit careful at your code heres how it shod go

$ignore = array('.', '..', '_vti_cnf', 'index.php', 'InstructSheet.txt', 'Edit Instructions Sheet.php', 'upload.php', 'admin', 'uploadstandards.php'); 

$result_array = array();
if ($handle = opendir('.')) 
{   
  while(false !== ($file = readdir($handle)))   
  {        
    if(!in_array ($file, $ignore))    
    { 			
      $result_array[] = $file;
    }   
  }  
  
  closedir($handle); 
}

sort($result_array);

foreach($result_array as $file)
{
  echo "\t<li>\n\t\t<a href=\"$file\">$file</a>\n<ul>$buffer</ul>\n\t</li>\n";	
}

Link to comment
https://forums.phpfreaks.com/topic/191770-sorting/#findComment-1010825
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.