注释和回复PHP应用程序的递归函数 [英] Recursive function for comment and reply PHP application

查看:122
本文介绍了注释和回复PHP应用程序的递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在概念上构造递归函数,以将评论附加到评论,回复的回复,回复的回复等.

这是我的评论表:

呈现应该时看起来像这样:

按现状,我可以呈现与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语句的末尾添加一个递归函数,但是在添加注释回复和每个回复的回复时,我还没有想出任何成功的方法. /p>

任何人都可以帮助我实现这一目标,或者为我指明正确的方向.我对递归函数并不完全熟悉.我已经对Internet和stackoverflow进行了一些扫描,以寻找有用的东西,但是还没有发现任何有用的东西.

我确实遇到过这篇文章,该文章建议使用分层数据库系统,但我想我更愿意为此使用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());
    }
}

解决方案

问题的核心是:

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

我认为,此功能在某处创建并运行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);

调用它

I am having difficulty conceptualizing a recursive function for appending replies to comments, replies to replies, replies to replies to replies, etc.

This is my comments table:

Which SHOULD look something like this when rendered:

As it stands I can render each comment associated with the article_id (excluding those that are NOT NULL, of course):

$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
    }

Now I'm assuming I need to add a recursive function at the end of the above foreach statement, but I have yet to come up with anything remotely successful in appending the comments replies and the replies to each reply.

Can anyone help me accomplish this or perhaps point me in the right direction. I am not entirely familiar with recursive functions. I have done some scanning of the internet and on stackoverflow looking for something that helps, but haven't found anything helpful yet.

I did come across this post which recommended using a hierarchical database system, but I think I would prefer to use a php recursive function for this.

Thanks.


edit


By using the below answers I am only returning the results first comment and then it iterates and displays that one comment indefinitely. I can't figure out why?

My 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()

and my PDO query:

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());
    }
}

解决方案

The core of the problem is this:

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

I assume, this function somwhere creates and runs SQL, that is similar to SELECT ... WHERE comments.article_id=$article_id. This is not sufficient - you need something like

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

that translates into SQL similar to SELECT ... WHERE comments.article_id=$article_id AND comments.comment_parent ($parent_id>0)?"=$parent_id":"IS NULL"

Now you can write your PHP function:

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);
    }
}

And finally call it with display_comments ($article_id);

这篇关于注释和回复PHP应用程序的递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆