These are pulled from database so that there is a $folders array, and each element has an array of ['id'], ['name'], ['parent'], and ['children']
When pulled out of the database, they are each checked to see if there is are any other entries whose parent field matches their id, and if it does, that child's id is added to an array in the folder's ['children'], if there are no children, then ['children'][0] = NULL
I output the array by itself like this first to see if I have all the data correct:
Each folder is displayed, and it's children's IDs are listed beneath it:
Code: Select all
<?php foreach($folders as $folder): ?>
<?=$folder['name']?><br />
<?php if($folder['children'][0] != NULL): ?>
<?php foreach($folder['children'] as $child): ?>
<?=$child?><br />
<?php endforeach; ?>
<?php endif; ?>
<br />
<?php endforeach; ?>Code: Select all
Root
140
141
Summer
142
Leadership
2009
143
Week 1but I need to display it in nested lists to show the user the structure. I made a recursive function and a function that calls the recursive function like this:
Code: Select all
function build_list($folders)
{
$theList = '';
return listBuilder($theList, $folders, $folders['0']);
}
function listBuilder($theList, $folders, $parent, $root = true)
{
if($root)
$theList .= '<li>'.$parent['name'].'</li>';
if($parent['children'][0] != NULL)
{
$theList .= '<ul>';
foreach($parent['children'] as $child)
{
$theList .= listBuilder($theList, $folders, $folders[$child]);
}
$theList .= '</ul>';
}
return $theList;
}Code: Select all
Root
Root
Summer
Root
Summer
2009
Root
Summer
2009
Week 1
Root
Root
Summer
Root
Summer
2009
Root
Summer
2009
Week 1
LeadershipCode: Select all
Root
Summer
2009
Week 1
Leadershipand I'm not sure why.
any ideas?