Created
September 4, 2014 21:55
-
-
Save hotestimator/149361c2b69a88d5f2c4 to your computer and use it in GitHub Desktop.
Nested tree list (recursive)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http://stackoverflow.com/questions/8020947/how-to-obtain-a-nested-html-list-from-objects-array-recordset | |
function render_tree($tree) | |
{ | |
echo '<ul>'; | |
foreach ($tree as $node) | |
{ | |
render_node($node); | |
} | |
echo '</ul>'; | |
} | |
function render_node($node, $level = 0) | |
{ | |
$inset = str_repeat(' ', $level); | |
echo $inset, '<li style="border:1px solid #eee;">', $node->name; | |
if (isset($node->children)) | |
{ | |
echo $inset, '<ul>'; | |
foreach ($node->children as $node) | |
{ | |
render_node($node, $level+1); | |
} | |
echo $inset, '</ul>', $inset; | |
} | |
echo '</li>'; | |
} | |
?> | |
<?php if ( ! empty($categories)): ?> | |
<div class="row"> | |
<?php render_tree($categories); ?> | |
</div> | |
<?php endif; ?> | |
$categories | |
Array | |
( | |
[1] => stdClass Object | |
( | |
[id] => 1 | |
[company_id] => 1 | |
[name] => Boys | |
[slug] => boys | |
[parent_id] => | |
[depth] => 0 | |
[lft] => 1 | |
[rgt] => 14 | |
[is_enabled] => 1 | |
[image_url] => boys.jpeg | |
[date_created] => 2012-04-17 16:16:59 | |
[date_modified] => 2014-09-03 18:20:50 | |
[order] => 2.00 | |
[children] => Array | |
( | |
[28] => stdClass Object | |
( | |
[id] => 28 | |
[company_id] => 1 | |
[name] => legos | |
[slug] => legos | |
[parent_id] => 1 | |
[depth] => 1 | |
[lft] => 4 | |
[rgt] => 5 | |
[is_enabled] => 1 | |
[image_url] => legos48.jpg | |
[date_created] => 2014-08-25 20:43:23 | |
[date_modified] => 2014-08-25 20:43:23 | |
[order] => | |
[children] => Array | |
( | |
) | |
) | |
[29] => stdClass Object | |
( | |
[id] => 29 | |
[company_id] => 1 | |
[name] => blocks | |
[slug] => blocks | |
[parent_id] => 1 | |
[depth] => 1 | |
[lft] => 6 | |
[rgt] => 11 | |
[is_enabled] => 1 | |
[image_url] => building_blocks22.jpg | |
[date_created] => 2014-09-02 16:34:12 | |
[date_modified] => 2014-09-02 16:34:12 | |
[order] => | |
[children] => Array | |
( | |
[30] => stdClass Object | |
( | |
[id] => 30 | |
[company_id] => 1 | |
[name] => fish | |
[slug] => fish | |
[parent_id] => 29 | |
[depth] => 2 | |
[lft] => 7 | |
[rgt] => 8 | |
[is_enabled] => 1 | |
[image_url] => fish.jpg | |
[date_created] => 2014-09-02 16:35:52 | |
[date_modified] => 2014-09-04 19:59:58 | |
[order] => | |
[children] => Array | |
( | |
) | |
) | |
) | |
) | |
) | |
) | |
[2] => stdClass Object | |
( | |
[id] => 2 | |
[company_id] => 1 | |
[name] => Girls | |
[slug] => girls | |
[parent_id] => | |
[depth] => 0 | |
[lft] => 15 | |
[rgt] => 16 | |
[is_enabled] => 1 | |
[image_url] => girls.jpg | |
[date_created] => 2012-04-17 16:17:10 | |
[date_modified] => 2014-09-03 18:20:57 | |
[order] => 1.00 | |
[children] => Array | |
( | |
) | |
) | |
[3] => stdClass Object | |
( | |
[id] => 3 | |
[company_id] => 1 | |
[name] => Boys and girls | |
[slug] => boys-and-girls | |
[parent_id] => | |
[depth] => 0 | |
[lft] => 17 | |
[rgt] => 20 | |
[is_enabled] => 1 | |
[image_url] => toys-for-girls-and-boys.jpg | |
[date_created] => 2012-04-17 16:17:31 | |
[date_modified] => 2014-09-03 18:26:16 | |
[order] => 3.00 | |
[children] => Array | |
( | |
[11] => stdClass Object | |
( | |
[id] => 11 | |
[company_id] => 1 | |
[name] => kenny | |
[slug] => kenny | |
[parent_id] => 3 | |
[depth] => 1 | |
[lft] => 18 | |
[rgt] => 19 | |
[is_enabled] => 1 | |
[image_url] => | |
[date_created] => 2012-08-24 13:30:37 | |
[date_modified] => 2012-08-24 13:30:37 | |
[order] => 3.50 | |
[children] => Array | |
( | |
) | |
) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment