我试图循环$feed
while循环中的所有评论,但是,它只显示一条评论,我做错了什么?如果我删除名称,他们就会循环。
如何在循环时命名注释?我认为这可以解决我的问题。以下是我目前的代码
$rows = array();
while($feed = mysqli_fetch_assoc($sth)) {
$query_ppimage = "SELECT id, post_id, relation, userID, file_format FROM media WHERE userID = '".$feed['userID']."' AND relation = 'profile_picture' UNION ALL SELECT -1 id, '55529055162cf' post_id, 'profile_picture' relation, '0' userID, 'jpg' file_format ORDER BY id DESC LIMIT 1";
$qppimg = $conn->query($query_ppimage);
while($ppimg = mysqli_fetch_assoc($qppimg)) {
$newrow = $feed;
if($feed['relation'] == 'page'){
$query_type = "SELECT name as 'page_name' FROM pages WHERE id = '".$feed['relation_id']."'";
$typeload = $conn->query($query_type);
while($type = mysqli_fetch_assoc($typeload)) {
$newrow['postto'] = $type;
}
}
$newrow['ppimage'] = $ppimg;
}
$comment_load = "SELECT * FROM media_comments WHERE post_id = '".$feed['post_id']."'";
$comments = $conn->query($comment_load);
while($com = mysqli_fetch_assoc($comments)) {
$newrow['comments'] = $com;
}
$rows[] = $newrow;
}
print json_encode($rows);
答案 0 :(得分:1)
根据我的评论,您面临的问题是,在while
循环的每次迭代中,您都覆盖$newrow['comments']
。因此,您只能在循环结束时获得单个值,并且它将是它在while循环中遇到的最终值。
您想要的是使用square bracket syntax of PHP arrays(即附加[]
)将评论附加到其中。
这是通过为数组赋值,在括号中指定键来完成的。也可以省略该键,从而产生一对空括号(
[]
)。$arr[key] = value; $arr[] = value; // key may be an integer or string // value may be any value of any type
如果$ arr尚不存在,则会创建
这意味着这样做:
while($com = mysqli_fetch_assoc($comments)) {
$newrow['comments'][] = $com;
}