Re: Recursive Directory Listing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 2008-10-30 at 15:28 -0400, Joe Schaeffer wrote:
> 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 <li> tags (and then close the corresponding <ul>
> 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 <jochem@xxxxxxxxxxxxx> 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:
> >>
> >> <ul>
> >>     <li>cats</li>
> >>     <li>dogs
> >>         <ul>
> >>             <li><beagles></li>
> >>             <li><collies>
> >>                 <ul><li>some...</li></ul>
> >>             </li>
> >>         </ul>
> >>     </li>
> >>     <li>horses</li>
> >> </ul>
> >>
> >> 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 <li></li> 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
> >>
> >
> >
> 
It is possible, just use recursive functions. For example, take the root
directory for the tree you want to display, loop through all the
directories. Outside of this loop, create your <ul> tags. Inside the
loop, check each directory to see if it has contents before you display
it, and if it does, call the current function with the current directory
as the argument. Then, the last item in your loop should be to display
the current directory inside an <li> tag.

I've used a derivative of this method for a recent project, but I had
two functions, one to store the tree into a multi-dimensional array so I
could sort the directories alphabetically, and then the second to
display the sorted list.


Ash
www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux