 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
10-11-2008, 03:05 AM
|
#1 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
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($file, strlen($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?
|
|
|
|
10-11-2008, 08:41 AM
|
#2 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
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.
|
|
|
|
10-11-2008, 02:58 PM
|
#3 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
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($dir, strlen($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?
|
|
|
|
10-11-2008, 03:05 PM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
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.
|
|
|
|
10-11-2008, 03:16 PM
|
#5 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
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"
|
|
|
10-12-2008, 12:17 AM
|
#6 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
Quote:
Originally Posted by xenon
Add a check before jumping into the foreach and it should be all good:
|
Perfect. It works great. Thank you for the help. :)
|
|
|
|
10-12-2008, 12:20 AM
|
#7 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
Quote:
Originally Posted by ReSpawN
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.
|
|
|
|
10-12-2008, 01:00 PM
|
#8 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
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.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
10-12-2008, 02:23 PM
|
#9 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
Quote:
Originally Posted by xenon
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.
__________________
"Life is a bitch, take that bitch on a ride"
|
|
|
10-13-2008, 06:34 PM
|
#10 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
Quote:
Originally Posted by ReSpawN
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?" 
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|