views:

42

answers:

1

Hi everyone,

I looked through the related questions for a similar question but I wasn't seeing quite what I need, pardon if this has already been answered already. In my database I have a list of records that I want represented to the user as files inside of a folder structure. So for each record I have a VARCHAR column called "FolderStructure" that I want to identify that records place in to the folder structure. The series of those flat FolderStructure string columns will create my tree structure with the folders being seperated by backslashes (naturally). I didn't want to add another table just to represent a folder structure... The 'file' name is stored in a separate column so that if the FolderStructure column is empty, the file is assumed to be at the root folder.

What is the best way to turn a collection of these records into a series of HTML UL/LI tags... where each LI represents a file and each folder structure being an UL embedded inside it's parent??

So for example:

file - folderStructure

foo -

bar - firstDir

blue - firstDir/subdir

would produce the following HTML:

<ul>
<li>foo</li>

    <ul>
     <li> bar </li>
      <ul>
       <li> blue </li>
      </ul>
     </ul>
</ul>

Thanks

A: 

Take a look at this article: http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/ for a start. It will help you convert your database into a tree structure in php. After that, you could use the function at the bottom with some modification... instead of indenting, have it print the ul tags, then for each final item, have it print it inside the li tags. And at the bottom, after the recursive call, put the closing ul tag.

Cahlroisse
Exactly what I needed, thank you :)
Greg Frommer