Am building application whitch will show family tree from database using recrusive function. For start everything work fantastic but my function not looping deeper and loop child of their child.
On image above looping must also loop Annie childs Steve and Rex.
Check my code:
<?php
$categories = $db->query("SELECT * FROM user");
$data = array();
// build menu
function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent'] == $parentId) {
$children = buildTree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
// Child recrusive looping
function recrusive_child($childs) {
if(isset($childs)) {
foreach ($childs as $child) {
echo "<li><a href='#'>".$child['username']."</a></li>";
}
}
}
while ($result = $categories->fetch_assoc()) {
$data[] = $result;
}
$tree = buildTree($data);
?>
<div class="tree">
<ul>
<?php foreach ($tree as $item): ?>
<?php if($item['parent'] == null): ?>
<li><a href=""><?= $item['username'];?></a>
<?php if($item['children']): ?>
<ul>
<?php recrusive_child($item['children']); ?>
</ul>
<?php endif; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
