【问题标题】:PHP Recursive Array for Blog Comments博客评论的 PHP 递归数组
【发布时间】:2022-01-14 19:06:57
【问题描述】:

大家好。

请,我正在写博客,我希望以自上而下的顺序显示博客 cmets 及其相关回复,即

Comment A

--->Sub-comments for Comment A

Comment B
---> Sub-comments for Comment B

按此顺序连续...

为了实现上述目的,我有 3 个 PHP 函数;

第一个函数检查评论(比如说,评论 A)是否有子评论并返回计数。

public function commentHasChildren($blog_id,$parent_id)
{
    $blog_id = $blog_id;
    $parent_id = $parent_id;
    $sql2 = "SELECT count(id) as counter FROM comment WHERE blog_id = '$blog_id' AND parent_id = '$parent_id' AND status = '1'";
    $result2 = $this->db_query($sql2);

    return $result2[0]['counter'];
}

第二个函数获取博客cmets和关联的子cmets

public function getBlogComment($blog_id,$parent_id = "")
{
    $id = $blog_id;
    $filter = ($parent_id == "")? "parent_id IS NULL":"parent_id = '$parent_id'";
    
    $sql = "SELECT * FROM comment WHERE blog_id = '$id' AND status = '1' AND $filter ORDER BY id DESC";
    $result = $this->db_query($sql);

    $output = [];
    if(count($result) > 0)
    {
        foreach($result as $row)
        {
            $counter = $this->commentHasChildren($id, $row['id']);
            $html = $this->getBlogCommentReplyForm($id);
            if ($counter > 0) { //if the blog has sub-comments, append the sub-comments to the comments
                    
                $html .= $this->getBlogCommentReplyForm($id, $row['id']);
            }
            $full_name = $row['full_name'];
            $content = $row['content'];
            $created = $row['created'];
            $parent_id = $row['id'];
            $output[] = [
                "blog_id"=>$id,
                "fullName"=>$full_name,
                "content"=>$content,
                "created"=>$created,
                "parent_id"=>$parent_id,
                "counter"=>count($counter),
                "children"=>$html, 
                "reply"=>$counter  
            ];
        }    
    }
    return $output;
}

第三个功能是显示博客cmets和相关的子cmets

public function getBlogCommentReplyForm($blog_id,$parent_id = "")
{
    $id = $blog_id;
    $filter = ($parent_id == "")? "parent_id IS NULL":"parent_id = '$parent_id'";
    
    $sql = "SELECT * FROM comment WHERE blog_id = '$id' AND status = '1' AND $filter ORDER BY id DESC";
    $result = $this->db_query($sql);

    $output = [];
    if(count($result) > 0)
    {
        foreach($result as $row)
        {
            $comments = '
                <li class="comment">
                    <article class="comment-body">
                        <footer class="comment-meta">
                            <div class="comment-author vcard">
                                <img src="avatar.png" class="avatar" alt="image">
                                <b class="fn">'.$row['full_name'].'</b>
                                <span class="says">says:</span>
                            </div>
                            <div class="comment-metadata">
                                <a href="#">
                                    <time>'.date('F d, Y', strtotime($row['created'])).' at '.date('h:m a', strtotime($row['created'])).'</time>
                                </a>
                            </div>
                        </footer>
                        <div class="comment-content">
                            <p>'.$row['content'].'</p>
                        </div>
                        <div class="reply">';?>
                        <?php $reply = (count($result) <= 1) ? ' reply':' replies'?>
                        <?=(count($result) > 0) ? '<kbd class="comment-reply-link">'.count($result).' '.$reply.' </kbd>':''?>
        <?php   $comments .= '
                            <button class="comment-reply-link show-reply-section show-reply-section'.$row['id'].'" data-comment_id="'.$row['id'].'">Reply</button>
                            <button class="comment-reply-link close-reply-section close-reply-section'.$row['id'].'" data-comment_id="'.$row['id'].'">Cancel</button>
                            <div class="comment-respond reply-section reply-section'.$row['id'].'" >
                                <h3 class="comment-reply-title">Leave a Reply</h3>
                                <form class="comment-form" id="post-reply'.$row['id'].'" data-id="'.$row['parent_id'].'" action="javascript:void(0)">
                                    <input type="hidden" name="op" id="op'.$row['id'].'" value="helper.replyComment">
                                    <input type="hidden" name="blog_id" id="blog_id'.$row['id'].'" placeholder="Comment Id" value="'.$blog_id.'">
                                    <input type="hidden" name="parent_id" id="parent_id'.$row['id'].'" value="'.$row['id'].'">
                                    <p class="comment-notes">
                                        <span id="email-notes">Your email address will not be published.</span>
                                            Required fields are marked
                                        <span class="required">*</span>
                                    </p>
                                    <p class="comment-form-comment">
                                        <label>Comment</label>
                                        <textarea name="content" id="content'.$row['id'].'" class="content" cols="45" rows="5" maxlength="65525" ></textarea>
                                    </p>
                                    <p class="comment-form-author">
                                        <label>Name <span class="required">*</span></label>
                                        <input type="text" id="full_name'.$row['id'].'" class="full_name" name="full_name">
                                    </p>
                                    <p class="comment-form-email">
                                        <label>Email <span class="required">*</span></label>
                                        <input type="email" id="username'.$row['id'].'" class="username" name="username">
                                    </p>
                                    <p class="comment-form-cookies-consent">
                                        <input type="checkbox" value="yes" name="wp-comment-cookies-consent" checked="" id="wp-comment-cookies-consent'.$row['parent_id'].'">
                                        <label for="wp-comment-cookies-consent">Save my name and email, and send me notifications on posts that are related to this one.</label>
                                    </p>
                                    <p class="form-submit">
                                        <button class="submit-reply btn btn-primary">
                                        <span class="fa fa-spinner fa-spin"></span> <o class="btn-text">Post Reply</o></button>
                                        <button class="comment-reply-link close-reply-section close-reply-section'.$row['id'].'" data-comment_id="'.$row['id'].'">Cancel</button>
                                        <span class="msg" style="color: white;"></span>
                                    </p>
                                </form>
                            </div>
                        </div>
                    </article>
                </li>';
            $output[] = [
                $comments
            ];
        }
    }
    return $comments;
}

第一个和第三个功能很好,我的问题是第二个功能

如何以递归方式重新迭代获取 cmets 和关联的子 cmets?

拜托,任何形式的帮助将不胜感激。

【问题讨论】:

    标签: php arrays multidimensional-array


    【解决方案1】:

    好的,经过大量研究和努力,我能够让功能完全按预期工作。

    这是我后来所做的;

    对于获取所有博客cmets和子cmets的函数

    public function getBlogComment($blog_id,$parent_id = "")
    {
        $id = $blog_id;
        $filter = ($parent_id == "")? "parent_id IS NULL":"parent_id = '$parent_id'";
        
        $sql = "SELECT * FROM blog_comment WHERE blog_id = '$id' AND status = '1' AND $filter ORDER BY id DESC";
        $result = $this->db_query($sql);
    
        $output = [];
        if(count($result) > 0)
        {
            foreach($result as $row)
            {
                $counter = $this->commentHasChildren($id, $row['id']);
                if((int)$counter == 1):
                    $reply = '<kbd class="comment-reply-link">'.$counter.' reply</kbd>';
                elseif(($counter > 1)):
                    $reply = '<kbd class="comment-reply-link">'.$counter.' replies</kbd>';
                else:
                    $reply = '';
                endif;
    
                $comments = '
                    <li class="comment">
                        <article class="comment-body">
                            <footer class="comment-meta">
                                <div class="comment-author vcard">
                                    <img src="../assets/img/icon.png" class="avatar" alt="image">
                                    <b class="fn">'.$row['full_name'].'</b>
                                    <span class="says">says:</span>
                                </div>
                                <div class="comment-metadata">
                                    <a href="#">
                                        <time>'.date('F d, Y', strtotime($row['created'])).' at '.date('h:m a', strtotime($row['created'])).'</time>
                                    </a>
                                </div>
                            </footer>
                            <div class="comment-content">
                                <p>'.$row['content'].'</p>
                            </div>
                            <div class="reply">
                                '.$reply.'
                                <button class="comment-reply-link show-reply-section show-reply-section'.$row['id'].'" data-comment_id="'.$row['id'].'">Reply</button>
                                <button class="comment-reply-link close-reply-section close-reply-section'.$row['id'].'" data-comment_id="'.$row['id'].'">Cancel</button>
                                <div class="comment-respond reply-section reply-section'.$row['id'].'" >
                                    <h3 class="comment-reply-title">Leave a Reply</h3>
                                    <form class="comment-form" id="post-reply'.$row['id'].'" data-id="'.$row['parent_id'].'" action="javascript:void(0)">
                                        <input type="hidden" name="op" id="op'.$row['id'].'" value="helper.replyComment">
                                        <input type="hidden" name="blog_id" id="blog_id'.$row['id'].'" placeholder="Comment Id" value="'.$blog_id.'">
                                        <input type="hidden" name="parent_id" id="parent_id'.$row['id'].'" value="'.$row['id'].'">
                                        <p class="comment-notes">
                                            <span id="email-notes">Your email address will not be published.</span>
                                                Required fields are marked
                                            <span class="required">*</span>
                                        </p>
                                        <p class="comment-form-comment">
                                            <label>Comment</label>
                                            <textarea name="content" id="content'.$row['id'].'" class="content" cols="45" rows="5" maxlength="65525" ></textarea>
                                        </p>
                                        <p class="comment-form-author">
                                            <label>Name <span class="required">*</span></label>
                                            <input type="text" id="full_name'.$row['id'].'" class="full_name" name="full_name">
                                        </p>
                                        <p class="comment-form-email">
                                            <label>Email <span class="required">*</span></label>
                                            <input type="email" id="username'.$row['id'].'" class="username" name="username">
                                        </p>
                                        <p class="comment-form-cookies-consent">
                                            <input type="checkbox" value="yes" name="wp-comment-cookies-consent" checked="" id="wp-comment-cookies-consent'.$row['parent_id'].'">
                                            <label for="wp-comment-cookies-consent">Save my name and email, and send me notifications on posts that are related to this one.</label>
                                        </p>
                                        <p class="form-submit">
                                            <button class="submit-reply btn btn-primary">
                                            <span class="fa fa-spinner fa-spin"></span> <o class="btn-text">Post Reply</o></button>
                                            <button class="comment-reply-link close-reply-section close-reply-section'.$row['id'].'" data-comment_id="'.$row['id'].'">Cancel</button>
                                            <span class="msg" style="color: white;"></span>
                                        </p>
                                    </form>
                                </div>
                            </div>
                        </article>';
                    if ((int)$counter > 0):
                        $comments .= $this->getBlogCommentReplyForm($id, $row['id']);
                    else:
                        $comments .= '</li>';
                    endif;
    
                $html = $comments;
    
                $output[] = [
                    "children"=>$html  
                ];
            }    
        }
        return $output;
    }
    

    对于返回所有相关子评论回复的另一个函数

    public function getBlogCommentReplyForm($blog_id,$parent_id)
    {
        $id = $blog_id;
        // $filter = ($parent_id == "")? "parent_id IS NULL":"parent_id = '$parent_id'";
        
        $sql = "SELECT * FROM blog_comment WHERE blog_id = '$id' AND status = '1' AND parent_id='$parent_id' ORDER BY id DESC";
        $result = $this->db_query($sql);
    
        $comments = '';
    
        if(count($result) > 0)
        {
            foreach($result as $row)
            {
                $counter = $this->commentHasChildren($id, $row['id']);
                if((int)$counter == 1):
                    $reply = '<kbd class="comment-reply-link">'.$counter.' reply</kbd>';
                elseif((int)$counter > 1):
                    $reply = '<kbd class="comment-reply-link">'.$counter.' replies</kbd>';
                else:
                    $reply = '';
                endif;
    
                $comments .='
                    <ol class="children" id="children'.$id.'">
                        <article class="comment-body">
                            <footer class="comment-meta">
                                <div class="comment-author vcard">
                                    <img src="../assets/img/icon.png" class="avatar" alt="image">
                                    <b class="fn">'.ucwords($row['full_name']).'</b>
                                    <span class="says">says:</span>
                                </div>
                                <div class="comment-metadata">
                                    <a href="#">
                                        <time>'.date('F d, Y', strtotime($row['created'])).' at '.date('h:m a', strtotime($row['created'])).'</time>
                                    </a>
                                </div>
                            </footer>
                            <div class="comment-content">
                                <p>'.$row['content'].'</p>
                            </div>
                            <div class="reply">
                                '.$reply.'
                                <button class="comment-reply-link show-reply-section show-reply-section'.$row['id'].'" data-comment_id="'.$row['id'].'">Reply</button>
                                <button class="comment-reply-link close-reply-section close-reply-section'.$row['id'].'" data-comment_id="'.$row['id'].'">Cancel</button>
                                <div class="comment-respond reply-section reply-section'.$row['id'].'" >
                                    <h3 class="comment-reply-title">Leave a Reply</h3>
                                    <form class="comment-form" id="post-reply'.$row['id'].'" data-id="'.$row['parent_id'].'" action="javascript:void(0)">
                                        <input type="hidden" name="op" id="op'.$row['id'].'" value="helper.replyComment">
                                        <input type="hidden" name="blog_id" id="blog_id'.$row['id'].'" placeholder="Comment Id" value="'.$blog_id.'">
                                        <input type="hidden" name="parent_id" id="parent_id'.$row['id'].'" value="'.$row['id'].'">
                                        <p class="comment-notes">
                                            <span id="email-notes">Your email address will not be published.</span>
                                                Required fields are marked
                                            <span class="required">*</span>
                                        </p>
                                        <p class="comment-form-comment">
                                            <label>Comment</label>
                                            <textarea name="content" id="content'.$row['id'].'" class="content" cols="45" rows="5" maxlength="65525" ></textarea>
                                        </p>
                                        <p class="comment-form-author">
                                            <label>Name <span class="required">*</span></label>
                                            <input type="text" id="full_name'.$row['id'].'" class="full_name" name="full_name">
                                        </p>
                                        <p class="comment-form-email">
                                            <label>Email <span class="required">*</span></label>
                                            <input type="email" id="username'.$row['id'].'" class="username" name="username">
                                        </p>
                                        <p class="comment-form-cookies-consent">
                                            <input type="checkbox" value="yes" name="wp-comment-cookies-consent" checked="" id="wp-comment-cookies-consent'.$row['parent_id'].'">
                                            <label for="wp-comment-cookies-consent">Save my name and email, and send me notifications on posts that are related to this one.</label>
                                        </p>
                                        <p class="form-submit">
                                            <button class="submit-reply btn btn-primary">
                                            <span class="fa fa-spinner fa-spin"></span> <o class="btn-text">Post Reply</o></button>
                                            <button class="comment-reply-link close-reply-section close-reply-section'.$row['id'].'" data-comment_id="'.$row['id'].'">Cancel</button>
                                            <span class="msg" style="color: white;"></span>
                                        </p>
                                    </form>
                                </div>
                            </div>
                        </article>';
                    if ((int)$counter > 0):
                        // var_dump(json_encode($row));
                        // var_dump($comments);
                        $comments .= $this->getBlogCommentReplyForm($id, $row['id']);
                        $comments .='</ol>';
                    else:
                        $comments .='</ol></li>';
                    endif;
    
    
            }
        }
        return $comments;
    }
    

    我希望你觉得这很有用...

    【讨论】:

      猜你喜欢
      • 2021-04-16
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 2013-04-16
      • 2013-11-21
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      相关资源
      最近更新 更多