1

I have function like this:

function show_comments(&$comments, $parent_id = 0) {
      $comments_list = "<ul>\n";
      foreach($comments as $comment) :
        if ($comment["parent_id"] != $parent_id)
          continue;
        $comments_list .= "<li>\n<h2>{$comment['id_comment']}</h2>\n";
        $comments_list .= "<p>$comment[body]</p>\n";


        $this->show_comments($comments, $comment['id_comment']);
        $comments_list .= "</li>\n";
      endforeach;
      $comments_list .= "</ul>\n";

      return $comments_list;
    }

At the moment this function is returning only one result (the first one). How can I bind all result and return them?

2
  • It should return all comments. Are you sure in $comments you have more than 1 comment and those comments' parent_id are different that $parent_id passed as parameter (or 0 if none given)? Commented Jul 2, 2014 at 9:00
  • There are 5 comments at the moment. First comment is the parent (with parent_id = 0), and next comment is the child of the previous one (comment 2 is child of comment 1, comment 3 is child of comment 2 and so on). Commented Jul 2, 2014 at 9:02

1 Answer 1

2

On line 10, you call show_comments() but you don't concatenate result.

$comments_list .= $this->show_comments($comments, $comment['id_comment']);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot mate, you saved me from headache. It works like a charm :) (I was trying to solve this for more than an hour :D ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.