我正在尝试输出带有孩子的用户树,但对于每个用户,它输出的树都是相同的...。有人可以告诉我如何为登录了孩子的用户回显树
第一个用户只有5个孩子,没有任何父母,每个孩子有5个孩子,并且列表还在继续
<?php
//fetch.php
include('php-includes/connect.php');
$query = "
SELECT * FROM user where parent_id='$parent_id'
";
$result = mysqli_query($con, $query);
//$output = array();
while($row = mysqli_fetch_array($result))
{
$sub_data["id"] = $row["email"];
$sub_data["name"] = $row["email"];
$sub_data["text"] = $row["email"];
$sub_data["parent_id"] = $row["parent_id"];
$data[] = $sub_data;
}
foreach($data as $key => &$value)
{
$output[$value["id"]] = &$value;
}
foreach($data as $key => &$value)
{
if($value["parent_id"] && isset($output[$value["parent_id"]]))
{
$output[$value["parent_id"]]["nodes"][] = &$value;
}
}
foreach($data as $key => &$value)
{
if($value["parent_id"] && isset($output[$value["parent_id"]]))
{
unset($data[$key]);
}
}
echo json_encode($data);
/*echo '<pre>';
print_r($data);
echo '</pre>';*/
?>
<script>
$(document).ready(function(){
$.ajax({
url: "fetch.php",
method:"POST",
dataType: "json",
success: function(data)
{
$('#treeview').treeview({data: data});
}
});
});
</script>
//表用户
db user{
id
email
parent_id
}
(id)(电子邮件)(parent_id)
1 user1 null
2 user2 user1
3 user3 user1
4 user4 user1
5 user5 user1
6 user6 user1
7 user7 user2
8 user8 user2
9 user9 user2
10 user10 user2
11 user11 user2
12 user12 user3
13 user13 user3