1

i have the below mysql records, so need to draw a tree structure in php by using the below mysql data, here NULL value represent the main parent node,

+----------------+----------------+
| child          | parent         |
+----------------+----------------+
| 216            |            103 |
| 217            |            216 |
| 88             |            216 |
| 102            |           NULL |
| 103            |            102 |
| 104            |            102 |
+----------------+----------------+

the output should be a below format

               102
              /   \ 
            103    104
            /
          216
         /  \
       217   218

please help me

2
  • 1
    Have you tried anything to achieve this? Can you show us some code please? Commented Oct 8, 2014 at 6:38
  • sorry i've no idea to achive this, Commented Oct 8, 2014 at 6:42

1 Answer 1

3

Function for making an array for child-parent:

function parseTree($tree, $root = null) {
    $return = array();
    # Traverse the tree and search for direct children of the root
    foreach($tree as $child => $parent) {
        # A direct child is found
        if($parent == $root) {
            # Remove item from tree (we don't need to traverse this again)
            unset($tree[$child]);
            # Append the child into result array and parse its children
            $return[] = array(
                'name' => $child,
                'children' => parseTree($tree, $child)
            );
        }
    }
    return empty($return) ? null : $return;    
}

For output:

function printtree($tree) {
    if(!is_null($tree) && count($tree) > 0) {
        echo '<ul>';
        foreach($tree as $node) {
            echo '<li>'.$node['name'];
            printTree($node['children']);
            echo '</li>';
        }
        echo '</ul>';
    }
}

Full PHP script:

<?php
$arr = array(
'216'=>103,
'217'=>216,
'88'=>216,
'102'=>NULL,
'103'=>102,
'104'=>102
);
function parseTree($tree, $root = null) {
    $return = array();
    # Traverse the tree and search for direct children of the root
    foreach($tree as $child => $parent) {
        # A direct child is found
        if($parent == $root) {
            # Remove item from tree (we don't need to traverse this again)
            unset($tree[$child]);
            # Append the child into result array and parse its children
            $return[] = array(
                'name' => $child,
                'children' => parseTree($tree, $child)
            );
        }
    }
    return empty($return) ? null : $return;    
}

function printtree($tree) {
    if(!is_null($tree) && count($tree) > 0) {
        echo '<ul>';
        foreach($tree as $b) {
            echo '<li>'.$b['name'];
            printtree($b['children']);
            echo '</li>';
        }
        echo '</ul>';
    }
}

printtree(parseTree($arr));
?>

Output:

  • 102
    • 103
      • 216
        • 217
        • 88
    • 104

small CSS one could use :-

li 
    { 
        position: relative; 
        margin-left: -15px;
        list-style: none;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

what u mean in $tree?
tree, in function parseTree is the array you want to convert into parent-child. :)
yeah it's fine, please say any possibilities to add css too :)
li { position: relative; margin-left: -15px; list-style: none; border-left:2px solid red; border-bottom:2px solid red; } please correct this
hi, it's not working with below array format `Array ( [192.168.10.216] => 192.168.40.103 [192.168.10.217] => 192.168.10.216 [192.168.10.88] => 192.168.10.216 [192.168.40.102] => NULL [192.168.40.103] => 192.168.40.102 [192.168.40.104] => 192.168.40.102 ) '

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.