我想显示一个文件夹树。我的文件夹具有安全访问权我在每个子文件夹herit之后仅为第一个文件夹定义安全性。
我想构建一个递归函数来显示所有可用的文件夹。
这是我的数据:
Folder 1 : {parent, id, label, security_access}
- Folder 2 : {parent, id, label}
- Folder 3 : {parent, id, label}
- Folder 4 : {parent, id, label}
Folder 5 : {parent, id, label, security_access}
- Folder 6 : {parent, id, label}
...
所以我获得了带有security_access的第一级文件夹(考虑$ free_folders变量),但是如何遍历子文件夹?只有1个数据库请求,我有很多文件夹,我不能有200个请求。 这是我的代码,我找不到何时使用递归:
$folders = $this->db->get_where('file', array('mime' => 'directory'))->result();
$folders_output = array();
foreach($folders as $folder)
{
$folders_all[$folder->parent_id][] = array(
'label' => $folder->name,
'parent' => $folder->parent_id,
'id' => $folder->id,
);
}
foreach ($folders as $folder)
{
// Get first level folders (parent_id = 1)
// $folder_free contains files accessible to users
if ($folder->parent_id == 1 && in_array($folder->id, $folders_free))
{
$folders_output[$folder->id]->parent = $folder->parent_id;
$folders_output[$folder->id]->label = $folder->name;
$folders_output[$folder->id]->id = $folder->id;
}
}
// Loop each first level folder
$_parent = 0;
foreach($folders_output as $id => $folder)
{
do
{
// ... 14th infinite loop
// How can I push each subfolders of my first level folder ?
$parent = $folders_all[$id][0]['parent'];
}
while ($_parent != 1);
}
// Show unordored list with folders - works !
$treeview = $this->treeview(1, 0, $folders_output);
我真的找不到最简单的解决方案..谢谢!
好的我认为没关系..我使用了递归函数,当我看到结果时,我认为并不是一种非常聪明的方式......:
foreach($folders_free as $folder)
{
$_results[] = $this->_dossiers($folders_all, $folder);
}
------------------------------------------------
private function _dossiers($array, $parent)
{
$results = array();
foreach ($array AS $entry)
{
if ($entry->parent == $parent)
{
$object = new stdClass();
$object->parent = $entry->parent;
$object->id = $entry->id;
$object->label = $entry->label;
$results[] = $object;
// Recursive call
$results[] = $this->_dossiers($array, $entry->id);
}
}
return $results;
}