CodeIgniter Forums

Full Version: Category tree
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Guys I tried to read stackoverflow but to no avail

How to make category tree similar to Internet shop.

-items
--tech
---phones
---pc
---consoles
--furniture
---tables
---beds


I got that in database must be something like this
ID, parentid, name

But how to get all itemsĀ in say --tech?
Or how to build tree..

Is there way without recursion?
You can do it without making that many round trips to the database. You need a group, parent_id, sub_parent_id, and levels. Then you can select all in the group instead of a get all call to the database.
So in this case group is the same as parent Id?
(10-14-2017, 01:54 AM)glorsh66 Wrote: [ -> ]So in this case group is the same as parent Id?

ID
The primary key that auto increments.

group
You can select (*) all from database where group = group. This allows you to reuse this function for unlimited amount of category trees.

Parent_Id
Is the Id of the first item returned on Level one so everything that is a sub of id(Item Number 1) would be tagged with it's Id in the parent_id field. This groups all sub menus together under one item at a time.

Sub_Parent_ID
This will start being assigned at Level 2 because Level one has no Parent_Id but the Item directly under it Level 2 will have a Parent_Id of the first Level item. Level 2's parent_id and sub_parent_id will always be the same id. At level Three the parent_id will still be the first item at level one but the Sub_parent_id will point to the items id on level 2. (My brain hurts now Smile )

Level
You need the level to loop through all the items on each level and you need it to get the next level by incrementing the value by 1 and recalling the submenu function.

All clear now?

I hate recursion but it works pretty good when you get it down