4

I want to make parent to be new "ul" and child "li" item,but obviously parent will have to be "li" and "ul" too at the same time,bcz i want to build "parent child relationship list" with tree principle, child will be a parent of another element too and that will create another subcategory and etc... so how would you have done it correctly? if you couldn't understand something... ask. In image i just display how it should look like,simple treeenter image description here

<?php
$tasks[] = array("id" => 1, "parent_id" => 0, "title" => 'task 1');
$tasks[] = array("id" => 2, "parent_id" => 1, "title" => 'sub task 1');
$tasks[] = array("id" => 3, "parent_id" => 1, "title" => 'sub task 2');
$tasks[] = array("id" => 4, "parent_id" => 2, "title" => 'sub sub task 1');
$tasks[] = array("id" => 5, "parent_id" => 2, "title" => 'task 2');
$tasks[] = array("id" => 6, "parent_id" => 2, "title" => 'sub task 3');
$branch = array();
function buildTree(array &$elements, $parentId = 0) {
    $branch = array();
    foreach ($elements as &$element) {
        if ($element['parent_id'] == $parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
                // $element['parent']=$element['title'];
                print_r($element['id']);
                foreach($element['children'] as $child){
                    if($element['id']==$child['parent_id']){
                    echo '<ul>';
                    echo '<li>'.$child['title'].'</li>';
                    echo '</ul>';
                    }
                }
            }
            $branch[$element['id']] = $element;
        }
    }
    return $branch;
}
print_r(buildTree($tasks));
?>
4
  • you're also producing invalid HTML markup Commented May 1, 2018 at 23:36
  • you just got rid of some html markup where you were echoing outside the <body></body> tags. Commented May 1, 2018 at 23:41
  • idea is that you won't have to return single element with query and loop through it,you just return everything and parse it with php and check relationship based on ID and parent_id... but displaying really is a messy problem Commented May 1, 2018 at 23:42
  • ah it was just written for test purposes... Commented May 1, 2018 at 23:43

1 Answer 1

3

You should try to group the childs of the items, so you can identify which items has "fathers" and which not. I made a sample with your code, but you can also some array_filter to improve it:

<?php
    $tasks[] = array("id" => 1, "parent_id" => 0, "title" => 'task 1');
    $tasks[] = array("id" => 2, "parent_id" => 1, "title" => 'sub task 1');
    $tasks[] = array("id" => 3, "parent_id" => 1, "title" => 'sub task 2');
    $tasks[] = array("id" => 5, "parent_id" => 2, "title" => 'task 2');
    $tasks[] = array("id" => 4, "parent_id" => 2, "title" => 'sub sub task 1');
    $tasks[] = array("id" => 6, "parent_id" => 2, "title" => 'sub task 3');
    $tasks[] = array("id" => 7, "parent_id" => 6, "title" => 'sub task of 6');
    $branch = array();

    function buildTree(array $elements, array $branch, $parentId=0) {
        // group elements by parents if it does not comes on the parameters
        if (empty($branch)) {
            $branch = array();

            foreach ($elements as $element) {
                $branch[$element["parent_id"]][$element["id"]] = $element;
            }
        }

        // echo the childs referenced by the parentId parameter
        if (isset($branch[$parentId])) {
            echo'<ul>';

            foreach ($branch[$parentId] as $keyBranch => $itemBranch) {
                echo '<li>'.$itemBranch['title'];
                buildTree($elements, $branch, $itemBranch["id"]); // iterate with the actual Id to check if this record have childs
                echo '</li>';
            }

            echo '</ul>';
        }
    }

    buildTree($tasks, array());
?>
Sign up to request clarification or add additional context in comments.

3 Comments

what if we need to grouping result by title after building the tree? so every child array should be grouped.
The you can do this using some combination of the sort functions that exists in PHP: w3schools.com/PHP/php_arrays_sort.asp
What if we need to create the tree for a specific category? let's say in the above example, create a tree only for "sub task 3"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.