I'm trying to loop all the comments from the $feed
while loop, however, its only displaying one comment, what am I doing wrong? If I remove the name, they loop.
How can I name the comment while loop? I think that may solve my problem. Below is my current code
$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);
As per my comment, the issue you are facing is that in each iteration in the while
loop, you are overwriting $newrow['comments']
. Therefore, you will only get a single value at the end of the loop, and it will be the final value it encounters in the while loop.
What you want is actually to append your comment to it, using the square bracket syntax of PHP arrays (i.e. by appending []
).
This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets (
[]
).$arr[key] = value; $arr[] = value; // key may be an integer or string // value may be any value of any type
If $arr doesn't exist yet, it will be created,
That means doing this:
while($com = mysqli_fetch_assoc($comments)) {
$newrow['comments'][] = $com;
}