我有一个帖子数组,我想把我的所有评论都显示在我的网站上。
每个帖子都有streamitem_id
streamdata表上的帖子ID为streamitem_id
已发布在指定帖子上的评论位于comment_streamitem
*
我之所以如此挣扎是因为,我想在posts数组中使用评论内容数组。但是两个顶级查询需要是DESC 4,然后当我使用无限滚动时,last_id被传回并在第二个查询中使用,其中收集并显示另外4个帖子。因此,每次发生这种情况时,我都希望所有评论都能附带指定的帖子。
我已经搜索了一些例子,并且在尝试之前尝试自己做,但是所有我设法真正发现它很接近,是关于WP的帖子,这对我没有好处。
使用阵列发帖查询
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));
评论查询和阵列
我真的不确定是否必须在没有DESC的情况下创建单独的查询,因为如果在循环中放入先前的查询中,这将限制返回的注释量!
$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));
答案 0 :(得分:0)
我现在有这个工作。我更改了注释查询,以将while循环中的streamitem_id与我的streamdata_comments表中的comment_streamiem行匹配。
然后我在post循环中添加了下面的代码及其数组,然后将"Streamitem_comments => $streamitem_comments,
添加到$json
数组中。
$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;
}