用于评论和回复PHP应用程序的递归函数

时间:2013-12-08 22:52:51

标签: php mysql recursion

我难以概念化递归函数以附加对评论的回复,对回复的回复,对回复回复的回复等。

这是我的评论表:

enter image description here

渲染时应该看起来像这样:

enter image description here

目前我可以呈现与article_id相关的每条评论(当然不包括那些NOT NULL):

$comments = $commentClass->fetch_article_comments($article_id);

foreach($comments as $comment) {

        $comment_id = $comment['comment_id'];   
        $member_id = $comment['member_id'];
        $comment_text = $comment['comment_text'];
        $comment_timestamp = timeAgo($comment['comment_timestamp']);  //get time ago

        //render comment using above variables
    }

现在我假设我需要在上面的foreach语句的末尾添加一个递归函数,但我还没有想出任何远程成功的附加评论回复和每个回复的回复

任何人都可以帮助我实现这个目标,或者让我指出正确的方向。我并不完全熟悉递归函数。我已经对互联网进行了一些扫描,并在stackoverflow上寻找有用的东西,但还没有找到任何有用的东西。

我确实遇到this post建议使用分层数据库系统,但我想我更喜欢使用php递归函数。

感谢。


修改


通过使用以下答案,我只返回结果第一个评论,然后迭代并无限期地显示一个评论。我无法弄清楚为什么?

我的php:

function display_comments($article_id, $parent_id=0, $level=0) {
    global $comment;
    global $member;
    global $signed_in;
    global $username;

    $comments = $comment->fetch_article_comments($article_id, $parent_id);

    foreach($comments as $data) {
        $comment_id = $data['comment_id'];   
        $c_member_id = $data['member_id'];
        $comment_text = $data['comment_text'];
        $comment_timestamp = timeAgo($data['comment_timestamp']);  //get time ago

        $member_data = $member->fetch_member_data($c_member_id);
        $c_member_username = $member_data['member_username'];
        $c_member_avatar = $member_data['member_avatar'];

                //render HTML here

                $parent = $data['comment_parent'];
                display_comments($article_id, $parent, $level+1);

    }//end foreach

}//end display_comments()

和我的PDO查询:

public function fetch_article_comments($article_id, $parent_id) { //$page = (int)(!isset($_GET['page'])) ? 0 : $_GET['page'];

    if ($parent_id > 0) {
        $parent_id = "= $parent_id";
    } else {
        $parent_id = "IS NULL";
    }

    $query = $this->db->prepare("SELECT * FROM `comments2` WHERE `article_id` = $article_id AND `comment_parent` $parent_id ORDER BY `comment_timestamp` DESC");

    try{
        $query->execute();
        $comments = $query->fetchAll();                                     //returns an array
        $query->closeCursor();

        return $comments;

    } catch(PDOException $e){
        die($e->getMessage());
    }
}

2 个答案:

答案 0 :(得分:2)

问题的核心是:

$comments = $commentClass->fetch_article_comments($article_id);

我假设,这个函数somwhere创建并运行SQL,类似于SELECT ... WHERE comments.article_id=$article_id。这还不够 - 你需要像

这样的东西
$comments = $commentClass->fetch_article_comments($article_id, $parent_id);

转换为类似于SELECT ... WHERE comments.article_id=$article_id AND comments.comment_parent ($parent_id>0)?"=$parent_id":"IS NULL"

的SQL

现在您可以编写PHP函数:

function display_comments ($article_id, $parent_id=0, $level=0) {
  $comments = $commentClass->fetch_article_comments($article_id, $parent_id);
  foreach($comments as $comment) {
        $comment_id = $comment['comment_id'];   
        $member_id = $comment['member_id'];
        $comment_text = $comment['comment_text'];
        $comment_timestamp = timeAgo($comment['comment_timestamp']);  //get time ago

        //render comment using above variables
        //Use $level to render the intendation level

        //Recurse
        display_comments ($article_id, $comment_id, $level+1);
    }
}

最后用display_comments ($article_id);

调用它

答案 1 :(得分:1)

您可以执行以下操作:

function getCommentsForParent($parent="") {
   echo "<div class=\"comment\">";
   $db = get("select your db logics where parent = $parent");
   foreach ($db as $comment) {
      echo "print your comment in a box and make a div around the hole comments"
      getCommentsForParent($comment['parent_id']);
   }
   echo "</div>";
}

使用getCommentsForParent()

调用函数

使用您的代码应该是:

function getCommentForParent($parent="") {
   global $commentsClass, $article_id;
   $comments = $commentClass->fetch_article_comments($article_id,$parent);
   foreach($comments as $comment) {
        $comment_id = $comment['comment_id'];   
        $member_id = $comment['member_id'];
        $comment_text = $comment['comment_text'];
        $comment_timestamp = timeAgo($comment['comment_timestamp']);  //get time ago
        getCommentForParent($comment['parent']);
    }
}

仔细观察,我调整了你的fetch article_comments。它现在有一个$ parent的监听器。确保当你的$ parent =“”时,该类只会选择空父项的注释。

使用getCommentsForParent()

进行调用

它将自动循环访问父级。当然,这是高度概念性的,但鉴于你的代码,你应该掌握它。