gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:46:33 25 4
gpt4 key购买 nike

我想根据数据库值 Expected output 在 php 中打印二叉树(下图)

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

这是我尝试过的,在这段代码中我想显示用户 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>

以上代码的输出是Here ,

根据数据库表,PX103是PX108和PX110的父代,PX105是PX107和PX111的父代,PX106是PX109的父代.但是PX108、PX110、PX107、PX111和PX109没有显示在树中。

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

最佳答案

<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`

关于php - 如何在 PHP 中使用 mysql 数据库创建二叉树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41407528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com