TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-11-2008, 03:05 AM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default 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?
benton is offline  
Reply With Quote
Old 10-11-2008, 08:41 AM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Try using glob. It sorts your entries by default, and has some more useful options. You only need to take care of your padding.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 10-11-2008, 02:58 PM   #3 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

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?
benton is offline  
Reply With Quote
Old 10-11-2008, 03:05 PM   #4 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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) ...

__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 10-11-2008, 03:16 PM   #5 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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 ) { ... } 
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 10-12-2008, 12:20 AM   #6 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
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.
benton is offline  
Reply With Quote
Old 10-12-2008, 12:17 AM   #7 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
Add a check before jumping into the foreach and it should be all good:
Perfect. It works great. Thank you for the help. :)
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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


All times are GMT. The time now is 10:13 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design