向使用jquery和php的注释系统添加回复


add reply to a comment system that using jquery and php

我试图创建一个使用facebook的评论系统。我使用php和jquery。我的代码运行得很好。我只想在里面添加一个回复系统。知道怎么做吗?

这是我的主页:wall.php

<script> 
  $(document).ready(function(){                           
  $("#comment_process").click(function(){
   if($("#comment_text").val() != ""){ 
    $.post("comments.php?action=post", { comment: $("#comment_text").val() }, function(data) {
        $(".comments").html(data);
        $("#comment_text").val("");
     });
    } 
  });   
 });   
</script>
<div class="comment_container">
<div class="comment_form">
<textarea id="comment_text" ></textarea>
<input type="button" id="comment_process" value="Post"/>
</div>
</div>
<div class="comments">  <?php include_once("comments.php");?> </div>
?>

这是评论.php

<?php
function getComments(){
$comments = "";
    // use desc order by date in order to display comments by date
    $sql = mysql_query("SELECT * FROM comments ORDER BY comment_date DESC ") or die (mysql_error());
    if(mysql_num_rows($sql) == 0){
            $comments = " <div class='each_comment'> There are no comments ...</div> ";
    }else{
        while ($row= mysql_fetch_assoc($sql)){          
            $comments .= "Says : <div class='each_comment'>  <small><em> ".$row['comment_date']." </em></small><br />".$row['comment']."</div> </br>"; 
        }
    }
    return $comments;  
}

function postComments($comment){
     $comment = mysql_real_escape_string(strip_tags($comment));
     $sql = mysql_query(" INSERT INTO `comments` (comment, comment_date) VALUES ('".$comment."', now()) ");
    return true;
}
if((isset($_GET['action'])) && ($_GET['action'] == "post")) {
    postComments($_POST['comment']);
}
echo getComments();
?>

Orbling说得对。让我向你展示我为我的博客网站做了什么,我正在ASP.NET MVC中工作,也许将来还会有其他网站。

我有两个名为ArticleComment的类,根据我的记忆,它们在php中具有以下属性。我的php 有点生疏

class Article {
public $article_id;
public $user_id;
public $article_date;
//... more properties, ctors, methods
}
class Comment {
public $comment_id;
public $article_id;
public $isRoot;
public $parent_id;
public $comment_date;
public $content;
//... more properties, ctors, methods
}

然后我会使用与你构建的类似的用户:

    function getComments($article_id){
$str = "";
// This should put all the comment in order by date but we still have to separate the //comments from the replies so we will add an additional nest foreach loop following an if //statement inside the main foreach loop. What will happen is the newest/oldest comment //will be the first one appended to the string and then it will go to the inner foreach //loop to see if the $parent_id = $comment_id and if it does it will append to string.
//I have a different div class for replies so they are indented. This may not be the most //practical way, but considering that most articles are going to have less that a 1000 //comments if your lucky this wont be too bad.
$q = $this->db->query("SELECT * FROM comments WHERE article_id = $article_id ORDER BY comment_date DESC") or die (mysql_error());
        if($q->num_rows() > 0):
            foreach($q->result() as $row):
                if($row->isRoot && $row->ParentId = null):
                // here you will append to the $str where you will create a div container                      //for each comment which is a root comment. I included a user avatar, user link, date //create, content, and on the bottom of my div I have an <span>expand replies</span> and a //<span>reply<span> where I use JQuery to toggle the replies on and off etc.
                // Now I do the same thing here as I will cycle through $q to find the //comments with the parent_id equal to the comment_id of this $row
//they will already be ordered by date. Cool thing is you could create an array of the reply and change the order so maybe you want newest comments on top but oldest replies on //top or I should say below the parent comment
                foreach($q->result() as $row2):
                   if($row->comment_id = $row2->parent_id)
                    // do the same thing except html format for reply (eg: indent div)
                   endif;
                endforeach;
            endforeach;
            }
        else:
            $str = "<div class='each_comment'> There are no comments ...</div> ";
        endif;
         echo htmlentities($str); 
    }

然后,您将不得不使用一些JQuery/Javascript,正如Orbling所建议的:一旦点击回复按钮,show(); hide();就会回复并使用slideDown(parent_id);之类的函数,其中<div>容器出现在其父注释下方。