2

I want to print binary tree (Below Image) in php from database value Expected output

And above is my database table structure Expected output

Here is what I tried, In this code I want to display binary tree of user id PX100

<?php 
include 'db_connect.php';

$user_id = 'PX100';
$select_right_tree = $con->prepare("SELECT * FROM tbl_user_postion WHERE user_id=:user_id");
$select_right_tree->bindParam(":user_id", $user_id);
$select_right_tree->execute();
$fetch_right_tree = $select_right_tree->fetch(PDO::FETCH_ASSOC);
$ex_pos_right = $fetch_right_tree['pos_right'];
$ex_pos_left = $fetch_right_tree['pos_left'];
 
function right_part($pos_right) {
    if(!empty($pos_right)) {
        $select_left_tree = $con->prepare("SELECT pos_right, pos_left FROM tbl_user_postion WHERE user_id=:user_id");
        $select_left_tree->bindParam(":user_id", $pos_right);
        $select_left_tree->execute();
        $fetch_left_tree = $select_left_tree->fetch(PDO::FETCH_ASSOC);
        $user_right = $fetch_left_tree['pos_right'];
        $user_left = $fetch_left_tree['pos_left'];
        return array($user_right, $user_left);
        if(!empty($user_right)) {
            right_part($user_right);
        }
        if(!empty($user_left)) {
            left_part($user_left);
        }
    }
}

function left_part($pos_left) { 
    if(!empty($pos_left)) {
        $select_left_tree = $con->prepare("SELECT pos_right, pos_left FROM tbl_user_postion WHERE user_id=:user_id");
        $select_left_tree->bindParam(":user_id", $pos_left);
        $select_left_tree->execute();
        $fetch_left_tree = $select_left_tree->fetch(PDO::FETCH_ASSOC);
        $user_right = $fetch_left_tree['pos_right'];
        $user_left = $fetch_left_tree['pos_left'];
        return array($user_right, $user_left);
        if(!empty($user_left)) {
            left_part($user_left);
        }
        if(!empty($user_right)) {
            right_part($user_right);
        }
    }
}
?>

<div class="tree">
    <ul>
        <li>
            <a href="#"><img src="image/user.png" ><br/><?php echo $user_id; ?></a>
            <ul>
                <li>
                    <a href="#"><img src="image/user.png"><br/><?php echo $ex_pos_right; ?></a>
                    <?php 
                    if(!empty($ex_pos_right))
                    {	
                        list($pos_right1, $pos_left1) = right_part($ex_pos_right);
                  	?>
                            <ul>
                                <li> <a href="#"><img src="image/user.png"><br/><?php echo $pos_right1; ?></a>  </li>
                                <li> <a href="#"><img src="image/user.png"><br/><?php echo $pos_left1; ?></a>  </li>
                            </ul>
                        <?php 
                        }
                        ?>
                    </li>
                    <li>
                        <a href="#"><img src="image/user.png"><br/><?php echo $ex_pos_left; ?></a> 
                        <?php 
                        if(!empty($ex_pos_left))
                        {	
                            list($pos_right2, $pos_left2) = left_part($ex_pos_left);
                  	    ?>
                            <ul>
                                <li> <a href="#"><img src="image/user.png"><br/><?php echo $pos_right2; ?></a>  </li>
                                <li>  <a href="#"><img src="image/user.png"><br/><?php echo $pos_left2; ?></a>  </li>
                            </ul>
                        <?php   
                        }
                        ?>
                    </li>
                </ul>
            </li>
        </ul>
    </div>

Output of the above code is Here,

From database table, PX103 is a parent of PX108 and PX110, PX105 is parent of PX107 and PX111 , PX106 is parent of PX109. But PX108, PX110, PX107, PX111 and PX109 are not displaying in tree.

Please help me to sort out this problem. Thankyou.

2
  • Depending on the table size it might be better to fetch the entire table and create a tree structure in only two loops. If a child always has a higher ID than its parent, you'll only need one loop. Commented Dec 31, 2016 at 14:03
  • @PaulSpiegel.Yes, childs always has a higher ID than its parent .Can u please exlpain me how can print it in while loop? Commented Dec 31, 2016 at 15:29

1 Answer 1

1
<table class="table table-bordered table-striped">
<tr>
<td width="950" > 
<?php $m_id=NULL;$m_id=$_SESSION['id'];
$width=580;
for($lopp=0;$lopp<=3;$lopp++){
$tree=mysqli_query($sql,"select * from tree where distributer_id='".$m_id."'");
$m_id_fetch=mysqli_fetch_assoc($tree);
$left_m_id=$riggt_m_id="";
$tree_detaila=mysqli_query($sql,"select Name,yo_id from registration where yo_id='".$m_id."'");
if(!$tree_detaila) print mysqli_error($sql);
$detail=mysqli_fetch_assoc($tree_detaila);
if(!empty($m_id_fetch['pos_left'])) $left_m_id=$m_id_fetch['pos_left'];if(!empty($m_id_fetch['pos_right'])) $riggt_m_id=$m_id_fetch['pos_right'];
?>                          <table class="table-responsive" width="850" border="0" align="center" cellpadding="0" cellspacing="0">
                            <tr>
                                <td colspan="2" valign="top" align="center"> <table class="table-responsive" width="<?php print $width; ?>" border="0" align="<?php if(!empty($m_id_fetch['pos_left']) && $lopp>0) print "left"; elseif(!empty($m_id_fetch['pos_right']) && $lopp>0) print "right"  ?> " cellpadding="0" cellspacing="0"> 
                               <?php if($lopp==0) { ?> <tr>
                                    <td width="<?php print $width; ?>" align="center"> <img src="dashboard/images/user.jpg" width="45" height="50" class="img-circle" />
                                  </td>
                                </tr> 
                                <tr>
                                    <td align="center"><?php print strtoupper($detail['Name']); ?></td>
                                </tr>
                                <tr>
                                    <td align="center"><?php print $detail['yo_id']; ?></td>
                                </tr><?php } else { print ' <tr>
                                                <td colspan="2" align="left"></td>
                                            </tr>'; }    ?>
                                <tr>
                                    <td align="center">     
                                            <tr>
                                                <td colspan="2" align="<?php if(!empty($m_id_fetch['pos_left']) && $lopp>0) print "left"; elseif(!empty($m_id_fetch['pos_right']) && $lopp>0) print "right"  ?>"><img src="img/p.png" width="<?php print $width=$width-90; ?>   " height="5" /></td>
                                            </tr>

                                                <?php 
    if($left_m_id!="" )  
        {
            $tree_detaila_l=mysqli_query($sql,"select Name,yo_id from registration where yo_id='".$m_id_fetch['pos_left']."'");

$detail_l=mysqli_fetch_assoc($tree_detaila_l);
        }
        if($riggt_m_id!="")
        {
        $tree_detaila_r=mysqli_query($sql,"select Name,yo_id from registration where yo_id='".$m_id_fetch['pos_right']."'");

$detail_r=mysqli_fetch_assoc($tree_detaila_r);
        }
     ?>

                                                       <tr>
                                               <?php if($left_m_id!="" ){ ?> <td align="left">
        <a href='#' class='tt'> <img src="dashboard/images/user.jpg" width="45" height="50" class="img-circle" /> </a></td><?php } ?>
                                    <?php if($riggt_m_id!="" ){ ?> <td align="right">
                                   <a href='#' class='tt'> <img src="dashboard/images/user.jpg" width="45" height="50" class="img-circle" /> </a></td><?php } ?>


                                            </tr>
                                            <tr>
        <?php if($left_m_id!="") { ?><td align=" left"><?php print $detail_l['Name']; ?></td><?php } ?>                                                                           <?php if($riggt_m_id!="") { ?><td align=" right"><?php print $detail_r['Name']; ?></td><?php } ?>


                                            </tr>
                                            <tr>
<?php if($left_m_id!="") { ?><td align=" left">
<?php print $detail_l['yo_id']; ?>
</td><?php } ?>                                                                                                  <?php if($riggt_m_id!="") { ?><td align=" right">
<?php print $detail_r['yo_id']; ?>
</td><?php } ?>

                                            </tr>


                                        </table></td>
                                </tr>
                            </table><?php $width=$width-100;$m_id=$m_id_fetch['pos_right']; } ?>
                                                                                                                  </td>
                            </tr>

                        </table>`enter code here`
Sign up to request clarification or add additional context in comments.

1 Comment

A little bit of editing would make the code much more readable.

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.