Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用mysql数据库在PHP中创建二叉树_Php_Mysql_Recursion_Binary Tree - Fatal编程技术网

如何使用mysql数据库在PHP中创建二叉树

如何使用mysql数据库在PHP中创建二叉树,php,mysql,recursion,binary-tree,Php,Mysql,Recursion,Binary Tree,我想用php从数据库值打印二叉树(下图) 上面是我的数据库表结构 这是我尝试过的,在这段代码中,我想显示用户id为PX100的二叉树 根据表的大小,获取整个表并仅在两个循环中创建树结构可能更好。如果一个子系统的ID总是高于其父系统的ID,那么您只需要一个循环。@PaulSpiegel。是的,childs的ID总是高于其父系统的ID。您能告诉我如何在while循环中打印它吗?稍加编辑会使代码更具可读性。 <table class="table tab

我想用php从数据库值打印二叉树(下图)

上面是我的数据库表结构

这是我尝试过的,在这段代码中,我想显示用户id为PX100的二叉树




根据表的大小,获取整个表并仅在两个循环中创建树结构可能更好。如果一个子系统的ID总是高于其父系统的ID,那么您只需要一个循环。@PaulSpiegel。是的,childs的ID总是高于其父系统的ID。您能告诉我如何在while循环中打印它吗?稍加编辑会使代码更具可读性。
<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`