添加回复使用jquery和php的评论系统

时间:2013-08-07 14:28:49

标签: php jquery

我试图创建一个使用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>

?>

这是comments.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();
?>

1 个答案:

答案 0 :(得分:0)

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>容器显示在其父注释下方。