簡體   English   中英

循環瀏覽所有主題注釋級別

[英]Loop through all threaded comments levels

我正在為我的網站制作評論系統。 我希望它有一個線程注釋系統,並且我試圖找出在不同級別的線程之間循環的最佳方法。 我真的不認為嵌套foreach循環是最好的方法,但是我似乎無法想到任何其他方法。 這是帶有嵌套循環的代碼(我知道它很草率,我仍處於開發階段):

function display_comments($blog_post_id, $limit = 0) {
    global $dbh, $user;
    $return = "";
    $comments = $this->get_post_comments($blog_post_id);
    foreach ($comments as $comment) {
        $comment_user = $user->get_userdata($comment['comment_userid']);
        $return .= '<div class="media">
  <a class="pull-left" href="#">
    <img class="media-object" src="holder.js/64x64" alt="">
  </a>
  <div class="media-body">
    <h4 class="media-heading">'.$comment_user['username'].'</h4>
    '.$comment['comment_text'];
        $replies = $this->get_comment_replies($comment['comment_id']);
        foreach ($replies as $reply) {
            $comment_user = $user->get_userdata($comment['comment_userid']);
            $return .= '<div class="media">
  <a class="pull-left" href="#">
    <img class="media-object" src="holder.js/64x64" alt="">
  </a>
  <div class="media-body">
    <h4 class="media-heading">'.$comment_user['username'].'</h4>
    '.$comment['comment_text'];
            if ($this->reply_count($reply['comment_id'])) {
                $replies_level_1 = $this->get_comment_replies($reply['comment_id']);
                foreach ($replies_level_1 as $reply_level_1) {
                    $comment_user = $user->get_userdata($comment['comment_userid']);
                    $return .= '<div class="media">
  <a class="pull-left" href="#">
    <img class="media-object" src="holder.js/64x64" alt="">
  </a>
  <div class="media-body">
    <h4 class="media-heading">'.$comment_user['username'].'</h4>
    '.$reply_level_1['comment_text'];
                }
                $return .= '</div></div>';
            }
        }
        $return .= '</div></div>';
    }
    return $return;
}

讓我知道您是否需要更多信息,感謝您的幫助!

編輯:

您可以在此處查看整個課程: http : //pastebin.com/ukRMJcA6

遞歸函數/方法將是您的解決方案。

了解遞歸

什么是PHP中的RECURSIVE函數?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM