在 PHP 中...将文件夹结构的字符串表示形式转换为嵌套数组的最佳方法
我浏览了类似问题的相关问题,但我没有看到我需要的东西,请原谅,如果已经回答了这个问题。在我的数据库中,我有一个记录列表,我希望将其作为文件夹结构内的文件呈现给用户。因此,对于每条记录,我都有一个名为“FolderStructure”的 VARCHAR 列,我想标识该记录放置在文件夹结构中。这些扁平的FolderStructure字符串列的系列将创建我的树结构,其中文件夹由反斜杠分隔(自然地)。我不想仅添加另一个表来表示文件夹结构...“文件”名称存储在单独的列中,因此如果“FolderStructure”列为空,则假定该文件位于根文件夹中。
将这些记录的集合转换为一系列 HTML UL/LI 标记的最佳方法是什么...其中每个 LI 代表一个文件,每个文件夹结构都是嵌入在其父级中的 UL?
例如:
file -folderStructure
foo
-bar -firstDir
blue -firstDir/subdir
将生成以下 HTML:
<ul>
<li>foo</li>
<ul>
<li> bar </li>
<ul>
<li> blue </li>
</ul>
</ul>
</ul>
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这篇文章:http://kevin.vanzonneveld.net/techblog/article/首先,convert_anything_to_tree_structs_in_php/。它将帮助您将数据库转换为 php 中的树结构。之后,您可以使用底部的函数进行一些修改...而不是缩进,让它打印 ul 标签,然后对于每个最终项目,让它在 li 标签内打印它。在底部,在递归调用之后,放置结束 ul 标记。
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.