1

我试图创建一个使用 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();
?>
4

1 回答 1

0

奥布林说得对。让我向您展示我在 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); 
    }

然后,您将不得不按照Orbling 的建议使用一些 JQuery/Javascript:show(); hide();回复并使用一个功能,例如slideDown(parent_id);一旦点击回复按钮,<div>容器就会出现在其父评论下方。

于 2013-12-14T20:17:58.287 回答