【问题标题】:Recursive function for comment and reply PHP application用于评论和回复 PHP 应用程序的递归函数
【发布时间】:2013-12-08 22:52:51
【问题描述】:

我很难概念化一个递归函数来附加对 cme​​ts 的回复、对回复的回复、对回复的回复等。

这是我的 cmets 表:

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

就目前而言,我可以呈现与 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 语句的末尾添加一个递归函数,但我还没有想出任何远程成功的方法来附加 cmets 回复和对每个回复的回复。

谁能帮我完成这个任务,或者指出我正确的方向。我对递归函数并不完全熟悉。我已经对互联网和 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());
    }
}

【问题讨论】:

    标签: php mysql recursion


    【解决方案1】:

    问题的核心是这样的:

    $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);调用它

    【讨论】:

    • 你的比我的好,probs :-)
    • 不应该 $comments = $commentClass->fetch_article_comments($article_id, $parent_id);$comments = $commentClass->fetch_article_comments($article_id, $parent_id = NULL); 以便它默认为 NULL?
    • 不,那只是在设置功能时。你不能在调用时这样做。
    • @NicholasLaw 不,不应该。 PHP null 和 MySQL null 是不同的,另外在 SQL 中你需要 WHERE ... IS NULLWHERE ...=0。所以我使用数字参数运行并在 SQL 生成中更改为 NULL:comments.comment_parent ($parent_id>0)?"=$parent_id":"IS NULL"
    • @EugenRieck 我收到一个 SQL 错误 SQLSTATE[HY093]: Invalid parameter number: no parameters were bound。我假设这是因为我正在使用 PDO 并且您的查询中的 ? 需要一个绑定变量?
    【解决方案2】:

    你可以这样做:

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

    仔细看,我调整了您的获取 article_cmets。它现在有一个 $parent 的监听器。确保当你的 $parent = "" 时,该类将只选择父级为空的 cmets。

    使用getCommentsForParent()调用

    它将自动循环通过父级。当然,这是高度概念化的,但鉴于您的代码,您应该掌握它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-14
      • 2014-03-30
      • 2022-01-13
      • 2020-06-18
      • 1970-01-01
      • 2011-07-05
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多