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 6469079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:57:02+00:00 2026-05-25T05:57:02+00:00

I want a table of comments like so id | comment | parent_id ————————–

  • 0

I want a table of comments like so

id | comment | parent_id
--------------------------
1    text1        0
2    text2        1
3    text3        2
4    text4        3
5    text5        3
6    text6        5

I want to construct an array displaying the hierarchy of the parents and children. The tree should go back a undetermined number of generations. I don’t want to use nesting foreach loops as I’m not sure how deep it goes. That is why I’m here, I’m not sure of the best practice for a problem like this. I also want to display the depth in the array. Below is an example. It doesn’t really relate to table above, but hopefully gives you an idea of what I need.

array(
    "depth"=> 4
    "parent" => array(
        "id"=> 1,
        "comment" => "sometext1"
        "child_count" => 2,
        "children" => array(
            0 => array(
                "id" => 2
                "comment" => "sometext2",
                "child_count" => 0,
                "children" => null
            ),
            1 => array(
                "id" => 3
                "comment" => "sometext3"
                "child_count" => 1,
                "children" => array(
                    0 => array(
                        "id" => 2
                        "comment" => "sometext2",
                        "child_count" => 2,
                        "children" => array(
                            0 => array(
                                "id" => 2
                                "comment" => "sometext2",
                                "child_count" => 0,
                                "children" => null
                            ),
                            1 => array(
                                "id" => 2
                                "comment" => "sometext2",
                                "child_count" => 1,
                                "children" => array(
                                    "id" => 2
                                    "comment" => "sometext2",
                                    "child_count" => 0,
                                    "children" => null
                                )
                            )
                    )
                )
            )
        )
    )
)

I was going to use foreach and do a SQL statement to retrive that parent/childs children. ie

$sql = "SELECT * FROM comments WHERE parent = $parent_id";

Im not really looking for the code for all this, just a pseudo code solution.

  • 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-05-25T05:57:02+00:00Added an answer on May 25, 2026 at 5:57 am

    This can be easily done in PHP… For this you need two arrays and a two while loops.

    This code will make a tree the way you wanted and for an undetermined depth and number of children.

    Pastebin to the working code.

    Using references, lets imagine everything is saved in an array $data with this structure: (id, comment, parent_id) where parent_id points to an id.

    Code to build the tree.

    $tree = array();
    reset($data);
    while (list($k, $v) = each($data))
        if (0 == ($pid = $v['parent_id']))
            $tree[$k] =& $data[$k]; else
            $data[$pid]['children'][$k] =& $data[$k];
    

    And to generate the depth and child count.

    reset($data);
    while (list($k, $v) = each($data))
        if (0 != $v['parent_id'])
        {
            $ref =& $data[$k];
            $depth = 0;
            do
            {
                if ($depth) $ref =& $data[$ref['parent_id']];
                $dre =& $ref['depth'];
                if (!isset($dre) || $dre <= $depth) $dre =  $depth++;
                if (isset($ref['children']))
                    $ref['child_count'] = count($ref['children']);
                    else
                {   
                    $ref['child_count'] = 0;
                    $ref['children'] = null;
                }   
            }   
            while ($ref['parent_id']);
        }
    

    All my code has been written on the fly and not even tested, so if there are any errors please forgive meeeeeeeee!!!!!!!!!!! ← Forget that, I tried it, fixed a couple of issues and now works perfectly.

    Note

    For this code to work, the index of every item has to be equal to its ID.

    The array I used to try the code.

    $data = array(
        '1' => array('id' => '1', 'comment' => 'a', 'parent_id' => 0),
        '2' => array('id' => '2', 'comment' => 'b', 'parent_id' => 0),
        '3' => array('id' => '3', 'comment' => 'c', 'parent_id' => 1),
        '4' => array('id' => '4', 'comment' => 'd', 'parent_id' => 1),
        '5' => array('id' => '5', 'comment' => 'e', 'parent_id' => 2),
        '6' => array('id' => '6', 'comment' => 'f', 'parent_id' => 2),
        '7' => array('id' => '7', 'comment' => 'g', 'parent_id' => 5),
        '8' => array('id' => '8', 'comment' => 'h', 'parent_id' => 7)
        );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want PLSQL to generate strings like: COMMENT ON COLUMN TABLE.COLUMN IS 'comment from
I've got a comments table, and each comment has a status like approved, awaiting
i already created a table for comments but i want to add the feature
I want to get table metadata for all table columns. Like type string(varchar2)/int/float/datetime and
I want to create something like reddit where they have comments, then replies to
I have a table structure like this: Comments table id review_id user_id created comments
I have a 'Comments' MySQL table that has the following fields: id text parent_id
I want to show only the top 5 comments for a specific post (Like
Let's say I have a Posts table and a Comments table. I want my
I want to think of a way to display and query comments. Each comment

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.