Welcome Guest, Not a member yet? Register   Sign In
Creating a view from an unlimited child/parent array
#1

[eluser]Unknown[/eluser]
Hi Guys,

I'm relatively new to codeiniter and I was wondering whether anyone could help me with this problem I have.

I am creating an web application which allows users to add unlimited roles which all have a parent child relationship and can be as many level deep as they require.

I have created a recursive function which creates an array of the parent child relationships for all the roles but I need to figure and what is best practice in codeigniter for passing this array to a view and displaying it correctly.

Here is an example of the array I am left with once I have passed data from the database to a helper to create a 'role tree'....

Code:
[role_tree] => Array
        (
            [0] => Array
                (
                    [role_id] => 1
                    [role_name] => tester
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [role_id] => 4
                                    [role_name] => test 2
                                )

                            [1] => Array
                                (
                                    [role_id] => 6
                                    [role_name] => uyuiy
                                )

                            [2] => Array
                                (
                                    [role_id] => 7
                                    [role_name] => uyuiy
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [role_id] => 10
                                                    [role_name] => bamm
                                                    [children] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [role_id] => 11
                                                                    [role_name] => testing tree
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [3] => Array
                                (
                                    [role_id] => 8
                                    [role_name] => uyuiy
                                )

                            [4] => Array
                                (
                                    [role_id] => 9
                                    [role_name] => test new
                                )

                        )

                )

            [1] => Array
                (
                    [role_id] => 5
                    [role_name] => test 3
                )

        )

)


What I want to do is create a view with each child indented from its parent in some kind of list format but I am not sure how to go about this now I have this array.

Thanks for looking.
#2

[eluser]wh1tel1te[/eluser]
Hi Paul,

Easiest way to do this in my opinion would be to create another recursive function to display the list. Here's my go at it:

Code:
<?php
function listroles($roles)
{
    ?>
    <ul>
    &lt;?php
    foreach($roles as $role)
    {
        ?&gt;
        <li>
            <p>&lt;?php echo $role['role_name']; ?&gt;</p>
            &lt;?php
            if ( ! empty($role['children']))
            {
                listroles($role['children']);
            }
            ?&gt;
        </li>
        &lt;?php
    }
    ?&gt;
    </ul>
    &lt;?php
}
?&gt;

Try passing your array into this function and see what comes out. Note, this code is untested!
#3

[eluser]Unknown[/eluser]
I think this has worked! Thanks for your help

Paul




Theme © iAndrew 2016 - Forum software by © MyBB