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

时间:2016-12-31 10:32:37

标签: php mysql recursion binary-tree

我想从数据库值Expected output

打印php中的二叉树(下图)

以上是我的数据库表结构Expected output

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

Private Sub btnPos7_Click(sender As Object, e As EventArgs) Handles btnPos7.Click
    currentButton = 7
    If started = True Then
        Call blockCheck()
        If blocked = False Then
            If (playerPos + 6) = 7 Or (playerPos - 6) = 7 Or (playerPos - 1) = 7 Then
                playerPos = 7
                btnPos7.BackColor = Color.Green
                For counter = 0 To 5
                    Call itemCheck()
                Next
            Else
                txtDisplay.Text += "You cannot move here"
            End If
        Else
            btnPos7.BackColor = Color.Black
            txtDisplay.Text += "The path is blocked. Find another way through"
        End If
    Else End If
    lblScore.Text = "Score: " & score
End Sub

上述代码的输出为Here

从数据库表中, PX103 是PX108和PX110的母公司, PX105 是PX107和PX111的母公司, PX106 是PX109的母公司。但是PX108,PX110,PX107,PX111和PX109没有显示在树上。

请帮我解决这个问题。 谢谢。

1 个答案:

答案 0 :(得分: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`