Joe Schaeffer wrote:
> Thanks, all, for pointing me to the SPL iterators. i've been hunting
> around and i've managed to come up with a working script. I know it
> could be better, though. I know this is terribly inefficient -- a lot
> of manual repetition and no recursion. Here's what I've got,
> commenting my concerns:
> <?php
> // start from the current directory
> $dirs = new DirectoryIterator('./');
> //open master ul
> echo '<ul>';
> foreach ( $dirs as $dir )
> {
> // only get dirs, exclude . and ..
> // seems like this should be a function...
> if (($dir->isDir()) AND ($dir != '.') AND ($dir != '..'))
> {
> // begin each qualifying dir with an li
> echo '<li>' . $dir;
> // don't close the li, check again, one level deeper
> // here's where i'm missing recursion, i think...
> $subDirs = new DirectoryIterator('./' . $dir .'/');
>
> if ($subDirs->valid())
> {
> // second verse, same as the first...
> echo '<ul>';
> foreach ($subDirs as $subDir)
> {
> if (($subDir->isDir()) AND ($subDir !=
> '.') AND ($subDir !='..'))
> {
> // i could manually add another
> level check, i guess...?
> echo '<li>'. $subDir . '</li>';
> }
> }
> // close second list
> echo '</ul>';
> }
> //close first-level item
> echo '</li>';
> }
> }
> //close master ul
> echo '</ul>';
> ?>
>
> this will work for me, but it's bloated, i know. and it's rendering
> unnecessary <ul /> in the code wherever there isn't a subdirectory (i
> can't get a hasChildren() check to work properly. I'd appreciate any
> feedback.
>
> I've been referring to the SPL documentation in the manual off
> php.net, and the examples at phpro.org. if there are any others out
> there, i'd be happy to keep digging!
>
Joe,
Here is a simplified recursive version of your above statement.
<?php
$dir = '.';
function displayDir($dir='.') {
$show = FALSE;
$results = glob($dir.'/*');
foreach ( $results AS $entry ) {
if ( is_dir($entry) && !in_array($entry, array('.', '..')) ) {
$dirs[] = $entry;
$show = TRUE;
}
}
if ( $show ) {
echo '<ul>';
foreach ( $dirs AS $entry ) {
echo '<li>', basename($entry);
displayDir($entry);
echo '</li>';
}
echo '</ul>';
}
}
displayDir($dir);
?>
Hope this helps
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php