Reputation: 813
How to display the category structure like WordPress using php?
Array
( [0] => stdClass Object ( [cat_id] => 64 [name] => Bathing Soap [slug] => bathing-soap [cat_taxonomy_id] => 65 [taxonomy] => product_cat [parent] => 63 )
[1] => stdClass Object
(
[cat_id] => 65
[name] => Chemical
[slug] => chemical
[cat_taxonomy_id] => 66
[taxonomy] => product_cat
[parent] => 64
)
[2] => stdClass Object
(
[cat_id] => 63
[name] => Soap
[slug] => soap
[cat_taxonomy_id] => 64
[taxonomy] => product_cat
[parent] => 0
)
)
Upvotes: 0
Views: 769
Reputation: 49
I have tried this below link. you can also try this below Link
http://stevenbuick.com/category-hierarchy-with-codeigniter-and-jstree/
Upvotes: 1
Reputation: 2719
In your example is arrays as you store this records in database.
For output tree structure you should convert it to tree structure.
For example:
Step 1 you can make when you load data from fro database. Steps 2 and 3 can make in one iteration (foreach)
After this your example will similar
array(
[0] => stdClass Object
(
[cat_id] => 0,
[childs] => array( [0]=>63 )
...
)
[63]=> stdClass Object
(
[cat_id] => 63,
[childs] => array( [0]=>64 )
...
)
Then you can output tree. Just begin with key = 0 and output all childs. For each child in first out title and then all childs. You should use recursion function.
Upvotes: 1