从数据库表中提取孙子时遇到一些问题:引荐。但是,看来我的代码实际上可以获取我的孙子的用户名。但是它没有出现在我的孩子的行下方,并且 INSPECT中没有错误。
请参阅我在此处附加的输出图像: 以下是我的代码输出的图像:
我使用Javascript来获取“引荐” 表中的数据
<!-- GENEOLOGY TREE -->
<div class="tree">
<?php $id = 1; ?>
<ul>
<li>
<div id="parent_account"><a href="#"><img src="user.png" alt="Avatar" style="width:50px"/></br>Parent<?php echo $id; ?></a></div>
</li>
</ul>
</div>
<!-- END OF GENOLOFY TREE -->
<script>
$(document).ready(function(){
// start get children
var _id = "<?php echo $id ?>";
var child = "";
var child_id = [];
var gchild_id = [];
var count = 0;
$.post('get_child_referrals.php', { id:_id }).done(function(response){
var response = jQuery.parseJSON(response);
var referred = response["referred"];
var referred_len = response["referred"].length;
if(referred_len > 0){
child += "<ul>";
for(var x = 0; x < referred_len; x++) {
console.log(referred[x]["usernamee"]);
count += 1;
child_id[x] = referred[x]["id"];
child += "<li id='child"+x+"'><a href="+x+"><img src='user.png' alt='Avatar' style='width:50px'/><br>Child "+referred[x]["usernamee"]+"</a></li>";
}
child += "</ul>";
console.log(child);
$("#parent_account").append(child);
// start get grandchildren
// console.log(child_id);
var gchild_len = child_id.length;
// console.log(gchild_len);
for(i = 0; i < gchild_len; i++){
gchild = "";
_id = child_id[i];
// console.log(_id);
$.post('get_child_referrals.php', { id:_id }).done(function(response){
gresponse = jQuery.parseJSON(response);
greferred = gresponse["referred"];
greferred_len = gresponse["referred"].length;
if(greferred_len > 0){
gchild += "<ul>";
for(y = 0; y < greferred_len; y++) {
gchild_id[y] = greferred[y]["id"];
console.log(greferred[y]["usernamee"]);
gchild += "<li id='gchild"+y+"'><a href="+y+"><img src='user.png' alt='Avatar' style='width:50px'/><br>Child "+greferred[y]["usernamee"]+"</a></li>";
}
gchild += "</ul>";
console.log($("#child"+i).length);
console.log(gchild);
$("#child"+i).append(gchild);
}
});
}
// end get grandchildren
}
});
// end get children
});
</script>
get_child_referrals.php
include('connectdb.php');
$response = array();
$response["referred"] = array();
$id = $_POST['id'];
$sql = "SELECT * FROM referrals WHERE referral_id='$id'"; // MY RECRUITERS
$result = $conn->query($sql);
while($data = $result->fetch_assoc()) {
array_push($response["referred"], $data);
}
echo json_encode($response);