In PHP... best way to turn string representation of a folder structure into nested array

Posted by Greg Frommer on Stack Overflow See other posts from Stack Overflow or by Greg Frommer
Published on 2010-04-14T05:19:01Z Indexed on 2010/04/14 5:23 UTC
Read the original article Hit count: 302

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about php

Related posts about recursion