TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Need alphabetized directory list (http://www.talkphp.com/general/3469-need-alphabetized-directory-list.html)

benton 10-11-2008 03:05 AM

Need alphabetized directory list
 
I am trying to read a list of directories from disk, indent them to show parent/child relationship and display in a dropdown list. Here's what I have so far:
PHP Code:

function GetStorageLocations($pathDir$store_locns, &$level)
{  
  if (
$handle opendir($pathDir))
  {
     while (
false !== ($file readdir($handle)))
     {
       if(
is_dir($pathDir.'/'.$file))
       {
         if (
$file != "." && $file != "..")
         {
            
$level++;
            
$fileShow str_pad($filestrlen($file) + (int)$level"-"STR_PAD_LEFT);
            
$store_locns[] = array('id' => $file'text' => $fileShow);
            
$store_locns GetStorageLocations($pathDir.'/'.$file$store_locns$level);
         }
       }     
     }
       
closedir($handle);
  }
  
$level--; 
  return 
$store_locns;


When I call that function and look at the results, they appear as
PHP Code:

parentA
-child 1 of parentA
--sub-directory in child 1
parentB
-child 1 in parentB
--sub-directory in child 1
---sub-directory in sub-directory of child 1 

That is how I want it to work. It gives a visual indication of the structure in the list.

The problem is with the sorting. From what I have read, readdir reads in the directories based on their datestamp but I want the order to be alphabetical. I've tried soting using this code
PHP Code:

function comparar($a$b) {
        return 
strnatcasecmp($a["id"], $b["id"]);
}
usort($store_locns"comparar"); 

That sorts them alphabettically but I lose the structure so that a child of one parent might be located under a different parent.

Is there a simpler way to do this or can anyone see a way to make it work correctly?

xenon 10-11-2008 08:41 AM

Try using glob. It sorts your entries by default, and has some more useful options. You only need to take care of your padding.

benton 10-11-2008 02:58 PM

Thank you. I wasn't aware of that function and it did fix the sorting problem. But I am getting an error now. I changed my function as shown below.

PHP Code:

function GetStorageLocations($curDir$store_locns, &$level)
{  
  
$dirs glob($curDir '/*'GLOB_ONLYDIR);    
  
$cur 0;
  foreach (
$dirs as $dir)
  {
     
$level++;
     
$dirShow str_pad($dirstrlen($dir) + (int)$level"-"STR_PAD_LEFT);
     
$store_locns[] = array('id' => $dir'text' => $dirShow);
     
$store_locns GetStorageLocations($dir$store_locns$level);
  }
  
$level--; 
  return 
$store_locns;


If I run it as shown, I get the error
Quote:

Warning: Invalid argument supplied for foreach() in...
The line it is failing at is
PHP Code:

foreach ($dirs as $dir

If I change that line to
PHP Code:

foreach ((array)$dirs as $dir

I don't get the error but there's no output either. Can someone please point out how to prevent that error or what I am doing wrong?

xenon 10-11-2008 03:05 PM

Perhaps the directory you're trying to read is empty, foreach expects an array, and glob returns a boolean false value on error. php.net itself says:

Quote:

Originally Posted by php.net
Note: On some systems it is impossible to distinguish between empty match and an error.

Add a check before jumping into the foreach and it should be all good:

PHP Code:

if(is_array($dirs) && count($dirs) > 0)
{
    foreach(
$dirs as $dir) ...



ReSpawN 10-11-2008 03:16 PM

The more simple approach is, to just define it as an array.
PHP Code:

$dirs = array(); 

Then, there is no need to check it because with the glob, you're just going to pump it full of data.

If you're getting the same error, just try the regular foreach like so.
PHP Code:

foreach ( (array)$dirs as $key => $value ) { ... } 


benton 10-12-2008 12:17 AM

Quote:

Originally Posted by xenon (Post 18786)
Add a check before jumping into the foreach and it should be all good:

Perfect. It works great. Thank you for the help. :)

benton 10-12-2008 12:20 AM

Quote:

Originally Posted by ReSpawN (Post 18787)
The more simple approach is, to just define it as an array.
PHP Code:

$dirs = array(); 

If you're getting the same error, just try the regular foreach like so.
PHP Code:

foreach ( (array)$dirs as $key => $value ) { ... } 


I had tried both of those before asking the question but they didn't work for some reason. I appreciate the suggestion though.

xenon 10-12-2008 01:00 PM

Respawn: your method simply won't work, because glob returns a result no matter what. So, the declaration you're thinking might be ok, is not. Why? Because that variable gets overwritten by the return value of the glob function. So:

PHP Code:

$dirs = array();
$dirs glob($curDir '/*'GLOB_ONLYDIR); 

is the same as without the declaration of the $dirs directory.

ReSpawN 10-12-2008 02:23 PM

Quote:

Originally Posted by xenon (Post 18791)
Respawn: your method simply won't work, because glob returns a result no matter what. So, the declaration you're thinking might be ok, is not. Why? Because that variable gets overwritten by the return value of the glob function. So:

PHP Code:

$dirs = array();
$dirs glob($curDir '/*'GLOB_ONLYDIR); 

is the same as without the declaration of the $dirs directory.

Hmmm ah well just trying to help. Taking part in the whole solutions it better than nothing hehe. On the side, I learned something out of this as well.

xenon 10-13-2008 06:34 PM

Quote:

Originally Posted by ReSpawN (Post 18792)
On the side, I learned something out of this as well.

Good to hear that. That's why I chose to explain the operation in detail, so that you understand WHY is it wrong and don't just wonder "what did I do wrong?" :-)


All times are GMT. The time now is 01:18 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0