0

I'm trying to create a array that look like this to create a file explorer experience in one of my apps.

(
    [0] => Array
        (
            [FolderID] => 1
            [FolderName] => folder 1
            [ParentFolder] => 
            [SubFolder] => Array
                (
                    [0] => Array
                        (
                            [FolderID] => 2
                            [FolderName] => folder 2
                            [SubFolder] => Array
                                (
                                    [0] => Array
                                        (
                                            [FolderID] => 4
                                            [FolderName] => folder 4
                                            [ParentFolder] => 2
                                        )

                                )
                        )

                    [1] => Array
                        (
                            [FolderID] => 3
                            [FolderName] => folder 3
                            [SubFolder] => Array
                                (
                                    [0] => Array
                                        (
                                            [FolderID] => 5
                                            [FolderName] => folder 5
                                            [ParentFolder] => 3
                                            [SubFolder] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            (
                                                                [FolderID] => 7
                                                                [FolderName] => folder 7
                                                                [ParentFolder] => 5
                                                            )
                                                        )
                                                )
                                        )
                                )
                        )

                )
        )

    [1] => Array
        (
            [FolderID] => 6
            [FolderName] => folder 6
            [ParentFolder] => 
        )

)

The array I get from my SQL look this


(
    [0] => Array
        (
            [FolderID] => 1
            [FolderName] => folder 1
            [ParentFolder] => 
        )

    [1] => Array
        (
            [FolderID] => 2
            [FolderName] => folder 2
            [ParentFolder] => 1
        )

    [2] => Array
        (
            [FolderID] => 3
            [FolderName] => folder 3
            [ParentFolder] => 1
        )

    [3] => Array
        (
            [FolderID] => 4
            [FolderName] => folder 4
            [ParentFolder] => 2
        )

    [4] => Array
        (
            [FolderID] => 5
            [FolderName] => folder 5
            [ParentFolder] => 3
        )

    [5] => Array
        (
            [FolderID] => 6
            [FolderName] => folder 6
            [ParentFolder] => 
        )

    [6] => Array
        (
            [FolderID] => 7
            [FolderName] => folder 7
            [ParentFolder] => 5
        )

)

I've been racking my brains for some time over this problem.

What I want to achieve is a browser like experiance that look like this https://bootsnipp.com/snippets/REyGB

|Folder 1
|  |___ Folder 2
|  |  |___ Folder 4
|  |___ Folder 3
|     |___ Folder 5
|        |___ Folder 7
|Folder 6

What is the loop (for or while) that I should be using? I'm kidding lost on how to do this.

Some code example will be appreciated. I'm also open to any suggestion if there's any better way or simpler way of doing this!

3
  • 1
    Maybe a simpler way would be do detect all sub folders given a parent. That's a simple query but that's all you need because the user will typically be browsing the tree by clicking on each node then all you need is to query (ajax or some other way) for that specific node. Commented Oct 2, 2022 at 19:14
  • @IT-goldman Thank you for the hint. I did exactly that. I realize that it was a way simpler solution. Commented Oct 3, 2022 at 0:43
  • stackoverflow.com/questions/29384548/… Commented Oct 3, 2022 at 1:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.