2016-03-21 75 views
-2

我有一个特点,在这里用户可以发布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>"; 
     } 
    } 

这仍然只显示一个注释,二来显示当有。

enter image description here

+4

您需要在while循环中回显注释。现在,你在每次迭代期间设置变量,但是在退出循环之后,只有最后一个在你的div中被回显。 –

+0

@AdamKonieska - 我曾尝试过,但它仍然只显示一行。我编辑了我的问题,以准确显示我在哪里回显了div。 – Freddy

+0

你正在设置'display:none;'在它们上面。你确定他们没有被回应? '$ num_of_comments'的价值是什么? –

回答

0

你为什么不使用AJAX?我做了一个使用评论(比如stackoverflow)的网站,我用了很多Ajax。当然,所有html元素的创建都将在js中完成,你将从php返回的将只是json(包含注释和其信息的内容)。 这将帮助您加载新的评论,而无需刷新页面(setInterval)。

对于答案,我发现东西都是陌生的,第一个是

if ($num_of_comments !=0 || $num_of_comments == 0){ 

这将是总是如此。第二件事是回声在同时段之外(这可能是回应一个评论的原因)。最后一件事情就是不显示任何你以HTML元素的样式放置的东西。所以我建议你在while模块中创建echo或者创建一个注释数组并创建一个迭代器。之后,尝试使用浏览器的检查工具来查看返回的代码源是否仅包含一个评论或更多。这将帮助您查看php是否有效。

相关问题