[PHP] Recursive Directory Listing
New to PHP development, new to the list; searched the archives but didn't find an answer (or at least nothing i could successfully adapt). I have a (readable) directory structure like so: ../navigation /cats /dogs /beagles /collies /some/other/dirs/ /horses I need to display those directories in nested html lists (marked up with css). Using PHP on the above, I'd like to produce the following HTML: cats dogs some... horses I'm able to display one level deep (cats, dogs, horses), but not nest the lists accordingly. Since this is for a navigation structure, obviously I'll have anchors in the lines. They'll be formatted like so href="index.php?section=dogs&chapter=collies&verse=some . Some details about my environment: 1) no files will be in the /navigation (or sub) dirs, but i'd (ideally) like to define a depth, to prevent >3-4 levels of navigation. 2) I can successfully exclude dirs (. and .. and anything else I define) 3) the actual PHP script probably won't be in the /navigation directory, so I'll need a defined starting path (ie, $root = /site/docs/includes/navigation/ or somesuch...). 4) no database access (otherwise this whole contraption wouldn't be an issue...) Any help is greatly appreciated, thank you in advance. --joe -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Recursive Directory Listing
Well, that makes things much easier (and that should teach me to rely on a 5-year-old O'Reilly book...)! Thanks for the help! I'm able to display all the contents of the correct dirs and subdirs, but I'm struggling with my implementation. Since I'm trying to nest unordered lists, I can't seem to 'catch' sub-directories to nest it within their parent tags (and then close the corresponding when necessary. Before I spend time trying to figure out how to chain the iterators or mine the returned arrays, is it fair to ask if this approach is even possible? Again, thanks for the point in the right direction! On Thu, Oct 30, 2008 at 1:30 PM, Jochem Maas <[EMAIL PROTECTED]> wrote: > Joe Schaeffer schreef: >> New to PHP development, new to the list; searched the archives but >> didn't find an answer (or at least nothing i could successfully >> adapt). >> >> I have a (readable) directory structure like so: >> >> ../navigation >> /cats >> /dogs >> /beagles >> /collies >> /some/other/dirs/ >> /horses >> >> I need to display those directories in nested html lists (marked up >> with css). Using PHP on the above, I'd like to produce the following >> HTML: >> >> >> cats >> dogs >> >> >> >> some... >> >> >> >> horses >> >> >> I'm able to display one level deep (cats, dogs, horses), but not nest >> the lists accordingly. Since this is for a navigation structure, >> obviously I'll have anchors in the lines. They'll be >> formatted like so >> href="index.php?section=dogs&chapter=collies&verse=some . >> >> Some details about my environment: >> 1) no files will be in the /navigation (or sub) dirs, but i'd >> (ideally) like to define a depth, to prevent >3-4 levels of >> navigation. >> 2) I can successfully exclude dirs (. and .. and anything else I define) >> 3) the actual PHP script probably won't be in the /navigation >> directory, so I'll need a defined starting path (ie, $root = >> /site/docs/includes/navigation/ or somesuch...). >> 4) no database access (otherwise this whole contraption wouldn't be an >> issue...) >> >> Any help is greatly appreciated, thank you in advance. > > RecurisiveDirectoryIterator and FilterIterator classes from SPL is something > that will get you home. > the archives and the intertubes offer examples (etc) on how to use these. > >> --joe >> > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Recursive Directory Listing
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: '; 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 '' . $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 ''; foreach ($subDirs as $subDir) { if (($subDir->isDir()) AND ($subDir != '.') AND ($subDir !='..')) { // i could manually add another level check, i guess...? echo ''. $subDir . ''; } } // close second list echo ''; } //close first-level item echo ''; } } //close master ul echo ''; ?> this will work for me, but it's bloated, i know. and it's rendering unnecessary 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! On 10/30/08, Jochem Maas <[EMAIL PROTECTED]> wrote: > Joe Schaeffer schreef: > > > Well, that makes things much easier (and that should teach me to rely > > on a 5-year-old O'Reilly book...)! Thanks for the help! > > > > I'm able to display all the contents of the correct dirs and subdirs, > > but I'm struggling with my implementation. Since I'm trying to nest > > unordered lists, I can't seem to 'catch' sub-directories to nest it > > within their parent tags (and then close the corresponding > > when necessary. Before I spend time trying to figure out how to chain > > the iterators or mine the returned arrays, is it fair to ask if this > > approach is even possible? > > > sure, RecursiveIteratorIterator or DirectoryTreeIterator both have > a getDepth() method which might come in useful. > > alternatively you could subclass either and define the beginChildren() and > endChildren() methods to output the open/close tags. > > have a hunt around here: http://www.php.net/~helly/php/ext/spl/ > > > > > > Again, thanks for the point in the right direction! > > > > > > > > On Thu, Oct 30, 2008 at 1:30 PM, Jochem Maas <[EMAIL PROTECTED]> wrote: > >> Joe Schaeffer schreef: > >>> New to PHP development, new to the list; searched the archives but > >>> didn't find an answer (or at least nothing i could successfully > >>> adapt). > >>> > >>> I have a (readable) directory structure like so: > >>> > >>> ../navigation > >>> /cats > >>> /dogs > >>> /beagles > >>> /collies > >>> /some/other/dirs/ > >>> /horses > >>> > >>> I need to display those directories in nested html lists (marked up > >>> with css). Using PHP on the above, I'd like to produce the following > >>> HTML: > >>> > >>> > >>> cats > >>> dogs > >>> > >>> > >>> > >>> some... > >>> > >>> > >>> > >>> horses > >>> > >>> > >>> I'm able to display one level deep (cats
Re: [PHP] Recursive Directory Listing
> Joe, > > Here is a simplified recursive version of your above statement. > > $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 ''; >foreach ( $dirs AS $entry ) { >echo '', basename($entry); >displayDir($entry); >echo ''; >} >echo ''; >} > } > displayDir($dir); > ?> > Hope this helps > -- > Jim Lucas Excellent, Jim. Thanks very much for your help. At the risk of straying too far from the original subject/call-for-help, is there a way to flag the *first* time the function runs through a creation? Though my design doesn't call for it now, I could see the value of styling the first level separately than the sub-directories. Again, thanks very much! --joe -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Recursive Directory Listing
>> I would say to do that with CSS, not building it into the output. That's actually how I've got things set up now -- I just rarely trust the house-of-cards that is nested lists in CSS (and I've gone and complicated things with first-child selectors all over the place)! Thanks, all, for the great help! --joe -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php