Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:38:26+00:00 2026-06-11T14:38:26+00:00

I created a nested comment library for Codeigniter and it’s almost working. I can’t

  • 0

I created a nested comment library for Codeigniter and it’s almost working.

I can’t seem to output the nested comments without echoing each <ul> or <li> element. I don’t want the library writing anything directly, I want to save it to a variable and return it so that I can echo it out inside a view.

Here is the library code:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Comments
{
    public $parents  = array();
    public $children = array();

    public function init($comments)
    {
        foreach ($comments as $comment)
        {
            if ($comment['parent_comment_id'] === NULL)
            {
                $this->parents[$comment['comment_id']][] = $comment;
            }
            else
            {
                $this->children[$comment['parent_comment_id']][] = $comment;
            }
        }

        $this->prepare($this->parents);

    } // End of init

    public function thread($comments)
    {
        if(count($comments))
        {
            echo '<ul>';

            foreach($comments as $c)
            {
                echo "<li>" . $c['text'];
                //Rest of what ever you want to do with each row

                if (isset($this->children[$c['comment_id']])) {
                    $this->thread($this->children[$c['comment_id']]);
                }

                echo "</li>";
            }
            echo "</ul>";
        }
    } // End of thread

    private function prepare()
    {
        foreach ($this->parents as $comment)
        {
            $this->thread($comment);
        }
    } // End of prepare

} // End of Comments class

The above code generates:

- Parent
    - Child
        - Child Third level
- Second Parent
    - Second Child

or in HTML:

<ul>
    <li>Parent
        <ul>
        <li>Child
            <ul>
                <li>Child Third level</li>
            </ul>
        </li>
        </ul>
    </li>
</ul>

<ul>
    <li>Second Parent
        <ul>
            <li>Second Child</li>
        </ul>
    </li>
</ul>

This is the correct HTML, but echoing them out is not desirable.

What I tried to do was:

public function thread($comments)
    {
        if(count($comments))
        {
            $output = '<ul>';

            foreach($comments as $c)
            {
                $output .= "<li>" . $c['text'];
                //Rest of what ever you want to do with each row

                if (isset($this->children[$c['comment_id']])) {
                    $this->thread($this->children[$c['comment_id']]);
                }

                $output .= "</li>";
            }
            $output .= "</ul>";

            echo $output;
        }
    } // End of thread

This doesn’t work as expected and it generates the following when echo’d:

- Child Third level
- Child
- Parent
- Second Child
- Second Parent

or the HTML:

<ul><li>Child Third level</li></ul>
<ul><li>Child</li></ul>
<ul><li>Parent</li></ul>
<ul><li>Second Child</li></ul>
<ul><li>Second Parent</li></ul>

This is obviously not desired since it’s not nesting the comments.

I’ve been stuck on this all day, anyone have a suggestion on how I can get the list to be generated properly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:38:27+00:00Added an answer on June 11, 2026 at 2:38 pm

    Try this:

    public function thread($comments,$output='')
    {
        if(count($comments))
        {
            $output. = '<ul>';
    
            foreach($comments as $c)
            {
                $output .= "<li>" . $c['text'];
                //Rest of what ever you want to do with each row
    
                if (isset($this->children[$c['comment_id']])) {
                    $output.=$this->thread($this->children[$c['comment_id']],$output);
                }
    
                $output .= "</li>";
            }
            $output .= "</ul>";
    
            return $output;
        }
    }
    

    I haven’t tested it, but it looks like it should work!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to created nested dictionary variable like the below, But I get
I am running rails 3.2 I have created a nested form (requests > tags)
because obj, the playingCard object is created inside a nested for loop does that
I followed this simple tutorial and created a nested repeater. This tutorial is simple
I have created a very simple nested loop example and am struggling to write
I am using a Singleton instance created out of a nested class. This instance
Anyone know how to create a threaded / nested comment system? I would like
I got two models: Post and Comment. Comment is a nested resource of Post:
In a database structure for Question/Answer/Comment like stachexchange projects, the database schema can be
I am making a nested comment system, and I have the following table: create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.