我有一个特点,在这里用户可以发布thoughts
用户可以添加评论到每个thought
它的工作方式是,当comments
链接点击,所有与该thought_id
相关的评论将被加载。创建为每行一个新的div(及其内容)数据库中的
这里是我的user_comments
表的结构:
id
body_of_msg
comment_posted_by
comment_posted_to
post_id (which is the id of a thought from the `user_thoughts` table)
考虑以下数据:
user_thoughts
表(1行):
id: 184
thought: "hello, this is a thought from anderson."
假设我有两行user_comments
表格:
id: 1
body_of_msg: Comment assigned to thought_id of 184
comment_posted_by: conor
comment_posted_to: anderson
post_id: 184
id: 2
body_of_msg: Another comment assigned to thought_id of 184
comment_posted_by: alice
comment_posted_to: anderson
post_id: 184
问题:目前,当我点击comments
链接时,只显示其中一条评论(最新的评论正在显示,所以在这种情况下,Alice的评论)。
下面是代码:
<?php
// Get the comments attached to a users post...
$get_comm = mysqli_query ($connect, "SELECT * FROM user_comments WHERE post_id='$thought_id' ORDER BY post_id DESC");
$num_of_comments = mysqli_num_rows($get_comm); // get number of comments for each post by post_id
// if there are comments for the post, get its content
if ($num_of_comments !=0 || $num_of_comments == 0){
while( $comment = mysqli_fetch_assoc ($get_comm)){
$comment_body = $comment['body_of_msg'];
$comment_posted_to = $comment['comment_posted_to'];
$comment_posted_by = $comment['comment_posted_by'];
$removed = $comment['comment_removed'];
}
echo "";
/** There are other divs and content echo'd here**/
////////////////////////////////////////////
// this is where each comment is displayed
echo "
<div id='toggleComment$thought_id' class='new_comment' style='display:none;'>
<br/><b><a href = 'profile_page/$comment_posted_by'> $comment_posted_by said</a></b>: $comment_body "; ?><?php
if ($comment_posted_by == $username){
echo "<a id='remove_comment' href = '/inc/remove_comment.php'> Delete </a>";
} echo "
</div>";
/////////////////////////////////////////////
}
?>
凡$thought_id
来自:
$count = mysqli_query ($connect, "SELECT * FROM user_thoughts");
while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) {
$thought_id = $row['id'];
}
我觉得: 这可能仅仅是我在努力寻找一个解决方案,但是,是不是每个评论都与另一个评论重叠?我的评论功能涉及动态地出现在想法下面的评论,所以我利用Javascript来实现这一点。想想这个区块可能正在被新的评论取代?
我已经试过:
while( $comment = mysqli_fetch_assoc ($get_comm)){
$comment_body = $comment['body_of_msg'];
$comment_posted_to = $comment['comment_posted_to'];
$comment_posted_by = $comment['comment_posted_by'];
$removed = $comment['comment_removed'];
// this is where each comment is displayed
echo "
<div id='toggleComment$thought_id' class='new_comment' style='display:none;'>
<br/><b><a href = 'profile_page/$comment_posted_by'> $comment_posted_by said</a></b>: $comment_body "; ?><?php
if ($comment_posted_by == $username){
echo "<a id='remove_comment' href = '/inc/remove_comment.php'> Delete </a>";
} echo "
</div>";
}
}
这仍然只显示一个注释,二来显示当有。
您需要在while循环中回显注释。现在,你在每次迭代期间设置变量,但是在退出循环之后,只有最后一个在你的div中被回显。 –
@AdamKonieska - 我曾尝试过,但它仍然只显示一行。我编辑了我的问题,以准确显示我在哪里回显了div。 – Freddy
你正在设置'display:none;'在它们上面。你确定他们没有被回应? '$ num_of_comments'的价值是什么? –