Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build the tree hierachy in one pass #210

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

acidvertigo
Copy link
Contributor

With this function can be easy to acess category data in one query and build category menu, count sub-categories etc..
for example the array resulting from this is:

Array
(
    [3] => Array
        (
            [children] => Array
                (
                    [10] => Array
                        (
                            [id] => 10
                            [parent_id] => 3
                            [name] => Action
                            [image] => subcategory_action.gif
                        )

                    [13] => Array
                        (
                            [id] => 13
                            [parent_id] => 3
                            [name] => Cartoons
                            [image] => subcategory_cartoons.gif
                        )

                    [12] => Array
                        (
                            [id] => 12
                            [parent_id] => 3
                            [name] => Comedy
                            [image] => subcategory_comedy.gif
                        )

                    [15] => Array
                        (
                            [id] => 15
                            [parent_id] => 3
                            [name] => Drama
                            [image] => subcategory_drama.gif
                        )

                    [11] => Array
                        (
                            [id] => 11
                            [parent_id] => 3
                            [name] => Science Fiction
                            [image] => subcategory_science_fiction.gif
                        )

                    [14] => Array
                        (
                            [id] => 14
                            [parent_id] => 3
                            [name] => Thriller
                            [image] => subcategory_thriller.gif
                        )

                )

            [id] => 3
            [parent_id] => 0
            [name] => DVD Movies
            [image] => category_dvd_movies.gif
        )

    [21] => Array
        (
            [id] => 21
            [parent_id] => 0
            [name] => Gadgets
            [image] => category_gadgets.png
        )

    [1] => Array
        (
            [children] => Array
                (
                    [17] => Array
                        (
                            [id] => 17
                            [parent_id] => 1
                            [name] => CDROM Drives
                            [image] => subcategory_cdrom_drives.gif
                        )

                    [4] => Array
                        (
                            [id] => 4
                            [parent_id] => 1
                            [name] => Graphics Cards
                            [image] => subcategory_graphic_cards.gif
                        )

                    [8] => Array
                        (
                            [id] => 8
                            [parent_id] => 1
                            [name] => Keyboards
                            [image] => subcategory_keyboards.gif
                        )

                    [16] => Array
                        (
                            [id] => 16
                            [parent_id] => 1
                            [name] => Memory
                            [image] => subcategory_memory.gif
                        )

                    [9] => Array
                        (
                            [id] => 9
                            [parent_id] => 1
                            [name] => Mice
                            [image] => subcategory_mice.gif
                        )

                    [6] => Array
                        (
                            [id] => 6
                            [parent_id] => 1
                            [name] => Monitors
                            [image] => subcategory_monitors.gif
                        )

                    [5] => Array
                        (
                            [id] => 5
                            [parent_id] => 1
                            [name] => Printers
                            [image] => subcategory_printers.gif
                        )

                    [7] => Array
                        (
                            [id] => 7
                            [parent_id] => 1
                            [name] => Speakers
                            [image] => subcategory_speakers.gif
                        )

                )

            [id] => 1
            [parent_id] => 0
            [name] => Hardware
            [image] => category_hardware.gif
        )

    [2] => Array
        (
            [children] => Array
                (
                    [19] => Array
                        (
                            [id] => 19
                            [parent_id] => 2
                            [name] => Action
                            [image] => subcategory_action_games.gif
                        )

                    [18] => Array
                        (
                            [id] => 18
                            [parent_id] => 2
                            [name] => Simulation
                            [image] => subcategory_simulation.gif
                        )

                    [20] => Array
                        (
                            [id] => 20
                            [parent_id] => 2
                            [name] => Strategy
                            [image] => subcategory_strategy.gif
                        )

                )

            [id] => 2
            [parent_id] => 0
            [name] => Software
            [image] => category_software.gif
        )

)

Based on this article found on web http://blog.ideashower.com/post/15147134343/create-a-parent-child-array-structure-in-one-pass

With this function can be easy to acess category data in one query and build category menu, count sub-categories etc..
for example the array resulting from this is:

```
Array
(
    [3] => Array
        (
            [children] => Array
                (
                    [10] => Array
                        (
                            [id] => 10
                            [parent_id] => 3
                            [name] => Action
                            [image] => subcategory_action.gif
                        )

                    [13] => Array
                        (
                            [id] => 13
                            [parent_id] => 3
                            [name] => Cartoons
                            [image] => subcategory_cartoons.gif
                        )

                    [12] => Array
                        (
                            [id] => 12
                            [parent_id] => 3
                            [name] => Comedy
                            [image] => subcategory_comedy.gif
                        )

                    [15] => Array
                        (
                            [id] => 15
                            [parent_id] => 3
                            [name] => Drama
                            [image] => subcategory_drama.gif
                        )

                    [11] => Array
                        (
                            [id] => 11
                            [parent_id] => 3
                            [name] => Science Fiction
                            [image] => subcategory_science_fiction.gif
                        )

                    [14] => Array
                        (
                            [id] => 14
                            [parent_id] => 3
                            [name] => Thriller
                            [image] => subcategory_thriller.gif
                        )

                )

            [id] => 3
            [parent_id] => 0
            [name] => DVD Movies
            [image] => category_dvd_movies.gif
        )

    [21] => Array
        (
            [id] => 21
            [parent_id] => 0
            [name] => Gadgets
            [image] => category_gadgets.png
        )

    [1] => Array
        (
            [children] => Array
                (
                    [17] => Array
                        (
                            [id] => 17
                            [parent_id] => 1
                            [name] => CDROM Drives
                            [image] => subcategory_cdrom_drives.gif
                        )

                    [4] => Array
                        (
                            [id] => 4
                            [parent_id] => 1
                            [name] => Graphics Cards
                            [image] => subcategory_graphic_cards.gif
                        )

                    [8] => Array
                        (
                            [id] => 8
                            [parent_id] => 1
                            [name] => Keyboards
                            [image] => subcategory_keyboards.gif
                        )

                    [16] => Array
                        (
                            [id] => 16
                            [parent_id] => 1
                            [name] => Memory
                            [image] => subcategory_memory.gif
                        )

                    [9] => Array
                        (
                            [id] => 9
                            [parent_id] => 1
                            [name] => Mice
                            [image] => subcategory_mice.gif
                        )

                    [6] => Array
                        (
                            [id] => 6
                            [parent_id] => 1
                            [name] => Monitors
                            [image] => subcategory_monitors.gif
                        )

                    [5] => Array
                        (
                            [id] => 5
                            [parent_id] => 1
                            [name] => Printers
                            [image] => subcategory_printers.gif
                        )

                    [7] => Array
                        (
                            [id] => 7
                            [parent_id] => 1
                            [name] => Speakers
                            [image] => subcategory_speakers.gif
                        )

                )

            [id] => 1
            [parent_id] => 0
            [name] => Hardware
            [image] => category_hardware.gif
        )

    [2] => Array
        (
            [children] => Array
                (
                    [19] => Array
                        (
                            [id] => 19
                            [parent_id] => 2
                            [name] => Action
                            [image] => subcategory_action_games.gif
                        )

                    [18] => Array
                        (
                            [id] => 18
                            [parent_id] => 2
                            [name] => Simulation
                            [image] => subcategory_simulation.gif
                        )

                    [20] => Array
                        (
                            [id] => 20
                            [parent_id] => 2
                            [name] => Strategy
                            [image] => subcategory_strategy.gif
                        )

                )

            [id] => 2
            [parent_id] => 0
            [name] => Software
            [image] => category_software.gif
        )

)
```
Based on this article found on web http://blog.ideashower.com/post/15147134343/create-a-parent-child-array-structure-in-one-pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant