recursive folder structure not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kellman
Forum Newbie
Posts: 2
Joined: Tue Jun 30, 2009 3:51 pm

recursive folder structure not working

Post by kellman »

I need to display the folders in an application I'm building that isn't actually folders, but rather entries in a database which each has an id, a name, and a parent name.
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; ?>
and it outputs:

Code: Select all

Root
140
141
 
Summer
142
 
Leadership
 
2009
143
 
Week 1
This is correct.

but 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;
}
but build_list() outputs this:

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
                                                        Leadership
but it should be outputting:

Code: Select all

Root
    Summer
        2009
            Week 1
    Leadership

and I'm not sure why.

any ideas?
User avatar
Salaria
Forum Commoner
Posts: 34
Joined: Fri Feb 13, 2009 2:50 am
Location: India
Contact:

Re: recursive folder structure not working

Post by Salaria »

Hi,

First add "else if" instead of "if" to make check on root folder here:

Code: Select all

if($root)
         $theList .= '<li>'.$parent['name'].'</li>';
  
     if($parent['children'][0] != NULL)
     {
change to

Code: Select all

 if($root)
         $theList .= '<li>'.$parent['name'].'</li>';
  
    else if($parent['children'][0] != NULL)
     {
I need input array to fix this fully :)
kellman
Forum Newbie
Posts: 2
Joined: Tue Jun 30, 2009 3:51 pm

Re: recursive folder structure not working

Post by kellman »

Actually, those two if statements are independent. The first one is in case I don't want Root to output as root, then in build list function, I can add a false to the list builder call. The second if is where the recursion takes place. If there was an else if, then recursion would only take place if I never output the names of the folders.

okay, so the array is as follows:
$folders is a simple number based array that holds all the "folders" that are in the database.
each element of $folders is an array whose elements are ['id'], ['name'], ['parent'] which is the id of the parent folder, and ['children'] which is an array of id's that are that folder's children. If it has no children, then $folders[#]['children'][0] = NULL.

In this case, the folders pulled out of the database look like this:

Code: Select all

$folders[0]['id'] = 0
$folders[0]['name'] = 'Root'
$folders[0]['parent'] = NULL
$folders[0]['children'][0] = 140
$fodlers[0]['children'][1] = 141
 
$folders[1]['id'] = 140
$folders[1]['name'] = 'Summer'
$folders[1]['parent'] = 0
$folders[1]['children][0] = 142
 
$folders[2]['id'] = 141
$folders[2]['name'] = 'Leadership'
$folders[2]['parent'] = 0
$folders[2]['children'][0] = NULL
 
$folders[3]['id'] = 142
$folders[3]['name'] = '2009'
$folders[3]['parent'] = 140
$folders[3]['children'][0] = 143
 
$folders[4]['id'] = 143
$folders[4]['name'] = 'Week 1'
$folders[4]['parent'] = 142
$folders[4]['children'][0] = NULL
I hope this makes the problem clearer.

Thanks.
Post Reply