Need help with Dynamic Directory Tree
Posted: Wed Nov 29, 2006 4:11 pm
I've been attempting to build a dynamic directory tree with limited success. Maybe my logic is wrong, or there's an easier way. I've been messing with this for days, and just can't get it right.
I have a couple fields in my database; FilePath and FileName
Now, let's say I pull all rows from my database, and this is what I get: (FilePath and FileName are put together, and it is sorted by FilePath, FileName).
DRINK/coffee.php
DRINK/milk.php
DRINK/SODA/DARK/cola.php
DRINK/SODA/DARK/rootbeer.php
DRINK/SODA/LIGHT/gingerale.php
FOOD/chicken.php
FOOD/chips.php
FOOD/FRUIT/apples.php
FOOD/FRUIT/oranges.php
FOOD/popcorn.php
My goal is to end up with this:
The hardest part is closing the list tags at the appropriate places. I just don't know how to create the loop for it. Also, I don't want to limit the directory depth.
I've tried pulling only DISTINCT FilePath's from the database. Going through each FilePath, breaking them apart and storing those parts in an array to get the levels right. Then, I check if the current level's path name matches the last. If not, I create a new list. Unfortunately, I don't know how to let it know when to close the list tag.
I appreciate any help I can get. Thanks.
I have a couple fields in my database; FilePath and FileName
Now, let's say I pull all rows from my database, and this is what I get: (FilePath and FileName are put together, and it is sorted by FilePath, FileName).
DRINK/coffee.php
DRINK/milk.php
DRINK/SODA/DARK/cola.php
DRINK/SODA/DARK/rootbeer.php
DRINK/SODA/LIGHT/gingerale.php
FOOD/chicken.php
FOOD/chips.php
FOOD/FRUIT/apples.php
FOOD/FRUIT/oranges.php
FOOD/popcorn.php
My goal is to end up with this:
Code: Select all
<li>DRINK
<ul>
<li>coffee.php</li>
<li>milk.php</li>
<li>SODA
<ul>
<li>DARK
<ul>
<li>cola.php</li>
<li>rootbeer.php</li>
</ul>
</li>
<li>LIGHT
<ul>
<li>gingerale.php</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>FOOD
<ul>
<li>chicken.php</li>
<li>chips.php</li>
<li>FRUIT
<ul>
<li>apples.php</li>
<li>oranges.php</li>
</ul>
</li>
<li>popcorn.php</li>
</ul>
</li>I've tried pulling only DISTINCT FilePath's from the database. Going through each FilePath, breaking them apart and storing those parts in an array to get the levels right. Then, I check if the current level's path name matches the last. If not, I create a new list. Unfortunately, I don't know how to let it know when to close the list tag.
I appreciate any help I can get. Thanks.