[英]Get all comments for each post json array
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
每个帖子都有一个streamitem_id
The posts id on the streamdata table is streamitem_id
流数据表上的帖子ID为streamitem_id
The comments that have been posted on a given post are on the streamdata_comments table under comment_streamitem
* 在给定帖子上发布的评论位于comment_streamitem
下的streamdata_comments表中*
The reason I'm struggling so much is because, I would like the comment content array inside the posts array. 我之所以如此努力,是因为我想要posts数组内的comment content数组。 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. 但是,前两个查询需要按4进行DESC,然后当我使用无限滚动时,将last_id传回并用于第二个查询,该查询收集并显示了另外4个帖子。 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. 我一直在搜索一些示例,并在提出问题之前尝试自己做,但我真正发现的所有内容都是关于WP的贴子,这对我没有好处。
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! 我真的不确定是否必须创建一个没有DESC的单独查询,因为如果将其放入while循环中的先前查询中,这将限制返回的注释量!
$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. 我更改了注释查询,以将while循环中的streamitem_id与streamdata_comments表中的comment_streamiem行进行匹配。然后,我将以下代码及其数组添加到了while循环的帖子中,然后在$json
添加了"Streamitem_comments => $streamitem_comments,
阵列。
$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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.