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

I looked through related questions for a similar question but I didn't see what I needed, sorry if this has already been answered already. In my database, I have a list of records that I want to present to the user as files inside a folder structure. So for each record, I have a VARCHAR column called "FolderStructure" which I want to define that the records are placed in the folder structure. A row of these flat columns of FolderStructure row will create my tree structure and folders will be separated by backslashes (naturally). I didn't want to add another table just to represent the folder structure ... The file name is stored in a separate column, so if the FolderStructure column is empty, the file is assumed to be in the root folder.

What is the best way to turn the collection of these entries into a series of HTML UL / LI tags ... where each LI is a file and each folder structure is a UL embedded within parent

So for example:

file - folderStructure

foo -

bar - firstDir

blue - firstDir / subdir

will generate the following HTML:

<ul>
<li>foo</li>

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

      

thanks

+2


a source to share


1 answer


Take a look at this article: http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/ to get started. This will help you convert your database to tree structure in php. After that you can use the function at the bottom with some modification ... instead of indentation, print the ul tags, then for each end element, print it inside the li tags. And at the bottom, after the recursive call, place a closed ul tag.



+2


a source







All Articles