php - Loop through all threaded comments levels -


i working on making comment system website. want have threaded comment system , trying figure out best way loop through different level threads. dont think having nested foreach loops best approach, cannot seem think of other way. here code nested loops (its sloppy know, still in development stages):

function display_comments($blog_post_id, $limit = 0) {     global $dbh, $user;     $return = "";     $comments = $this->get_post_comments($blog_post_id);     foreach ($comments $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 $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 $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; } 

let me know if need anymore info , help!

edit:

you can see entire class here of need to: http://pastebin.com/ukrmjca6

recursive function/method solution.

understanding recursion

what recursive function in php?


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -