I have a posts array that I would like to get all my comments into to show up on within my site.
Each post has a streamitem_id
The posts id on the streamdata table is streamitem_id
The comments that have been posted on a given post are on the streamdata_comments table under comment_streamitem
*
The reason I'm struggling so much is because, I would like the comment content array inside the posts array. But the two top queries need to be DESC by 4 and then when I use my infinite scroll the last_id is passed back and used in the second query where another 4 posts are collected and shown. So each time this happens, I would like all comment's to come with their designated posts.
I've searched for some examples as I would and try to do it myself before asking, but all I managed to really find that is close, is posts about WP, which is no good to me.
POST QUERY WITH ARRAY
if ($last_id==0){
$query = "SELECT *
FROM streamdata m
JOIN streamdata_comments t1
WHERE
m.streamitem_id=t1.comment_streamitem
AND
t1.comment_poster=$user1_id
GROUP BY m.streamitem_id
ORDER BY t1.comment_id DESC LIMIT 4";
$result = mysqli_query($mysqli, $query) or die('Error: ' .mysqli_error($mysqli));
}else{
$testa=$_POST['last_id'];
$query = "SELECT *
FROM streamdata m
JOIN streamdata_comments t1
WHERE
m.streamitem_id=t1.comment_streamitem
AND
t1.comment_poster=$user1_id
AND
t1.comment_id < $testa
GROUP BY m.streamitem_id
ORDER BY t1.comment_id DESC LIMIT 4";
$result = mysqli_query($mysqli, $query) or die('Error: ' .mysqli_error($mysqli));
}
$rowcount = mysqli_num_rows($result);
$json = array();
while ($row = mysqli_fetch_assoc($result)) {
$json[] = array(
'streamitem_id' => $row['streamitem_id'],
'streamitem_content' => $row['streamitem_content'],
}
echo json_encode(array('posts' => $json, 'last_id' => $last_id));
COMMENTS QUERY AND ARRAY
I was really unsure whether or not I had to create a separate query without DESC as this would limit the amount of comments returned if put inside the previous queries while loop!
$callcomments = "SELECT *
FROM streamdata m
JOIN streamdata_comments t1
WHERE
t1.comment_streamitem=m.streamitem_id
AND
t1.comment_targetuser=$user1_id
ORDER BY t1.comment_id DESC ";
$check1comments = mysqli_query($mysqli, $callcomments) or die(mysqli_error($mysqli));
$comments = array();
while($rowcomments = mysqli_fetch_array($check1comments)){
$comments[] = array(
'comment_id' => $rowcomments['comment_id'],
'comment_content' => $rowcomments['comment_content'],
);
}
echo json_encode(array('posts' => $comments));
I now have this working. I changed the comments query to match streamitem_id from the while loop against the comment_streamiem row within my streamdata_comments table., I then added the below code and its array inside the posts while loop and then added "Streamitem_comments => $streamitem_comments,
into the $json
array.
$callcomments = "SELECT * FROM streamdata_comments WHERE comment_streamitem =
".$row['streamitem_id']."";
$check1comments = mysqli_query($mysqli, $callcomments) or
die(mysqli_error($mysqli));
$comment_num=mysqli_num_rows($check1comments);
$commenting = array();
while ($rowcomments = mysqli_fetch_assoc($check1comments)){
$commentsArray = array(
'comment_id' => $rowcomments['comment_id'],
'comment_content' => $rowcomments['comment_content'],
'comment_streamitem' => $rowcomments['comment_streamitem']
);
$commenting[] = $commentsArray;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.