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

  • Home
  • SEARCH
  • 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 7870793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:43:19+00:00 2026-06-03T01:43:19+00:00

I would like to diplay comments on my site like this: <li>Parent <ul> <li>child

  • 0

I would like to diplay comments on my site like this:

<li>Parent
    <ul>
        <li>child one</li>
        <li>child two
            <ul>
                <li>grandchild</li>
                <li>other grandchild</li>
            </ul>
        </li>
     </ul>
<li>Another parent with no children</li>
<li>

I have read the following article, however it doesn’t use <li>. So is there a way to display comments like I’ve done before with an array like so? Thanks.

$comments = array(
      array('id'=>1, 'parent_id'=>NULL,   'text'=>'Parent'),
      array('id'=>2, 'parent_id'=>1,      'text'=>'Child'),
      array('id'=>3, 'parent_id'=>2,      'text'=>'Child Third level'),
      array('id'=>4, 'parent_id'=>NULL,   'text'=>'Second Parent'),
      array('id'=>5, 'parent_id'=>4,      'text'=>'Second Child')
);
  • 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-03T01:43:20+00:00Added an answer on June 3, 2026 at 1:43 am

    I asssume your comment table has id, parent_id, comment, … and my suggestion goes like this;

    Select you comments like;

    $sql = "SELECT *FROM comments ORDER BY id DESC";
    
    $rows = mysql_query($sql);
    

    And next step is array operations.You can see the following code below and try working demo here;

    $rows = your_select_result;//I assumed that you have done these stuffs
    $comments = $row;
    /**
    This is test data, please remove this array while you are
    running own application.Since you will use the data one you get your db
    **/
    $comments = array(
        1 => array('id' => 1, 'parent_id' => 0, 'childs' => array()),
        2 => array('id' => 2, 'parent_id' => 0, 'childs' => array()),
        3 => array('id' => 3, 'parent_id' => 0, 'childs' => array()),
        5 => array('id' => 5, 'parent_id' => 0, 'childs' => array()),
        11 => array('id' => 11, 'parent_id' => 0, 'childs' => array()),
        17 => array('id' => 17, 'parent_id' => 0, 'childs' => array()),
        23 => array('id' => 23, 'parent_id' => 0, 'childs' => array()),
        28 => array('id' => 28, 'parent_id' => 0, 'childs' => array()),
        4 => array('id' => 4, 'parent_id' => 1, 'childs' => array()),
        6 => array('id' => 6, 'parent_id' => 1, 'childs' => array()),
        8 => array('id' => 8, 'parent_id' => 2, 'childs' => array()),
        9 => array('id' => 9, 'parent_id' => 2, 'childs' => array()),
        7 => array('id' => 7, 'parent_id' => 3, 'childs' => array()),
        12 => array('id' =>12, 'parent_id' => 7, 'childs' => array()),
        13 => array('id' => 13, 'parent_id' => 12, 'childs' => array()),
    );
    
    /** Comment prepare start */
    foreach ($comments as $k => &$v) {
        if ($v['parent_id'] != 0) {
            $comments[$v['parent_id']]['childs'][] =& $v;
        }
    }
    unset($v);
    
    foreach ($comments as $k => $v) {
        if ($v['parent_id'] != 0) {
            unset($comments[$k]);
        }
    }
    
    /** Comment prepare end */
    
    //Your indent pattern
    function indent($size) {
        $string = "";
        for ($i = 0; $i < $size; $i++) {
            $string .= "#";
        }
        echo $string; 
    }
    
    
    function printComments($comments, $indent = 0) {
        foreach ($comments as $comment) {
            echo indent($indent + 1).' I am comment '.$comment['id']."\n";
            if (!empty($comment['childs'])) {
                printComments($comment['childs'], $indent + 1);
            }
            }
    }
    
    
    printComments($comments);
    

    For demo please see here

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

Sidebar

Related Questions

I would like your comments and suggestion on this. I am doing the pagination
I have a div on my page where I would like to display comments
Essentially, I have a profile page with comments that get displayed. I would like
I have two tables in my database and I would like to join them.
I would like to diplay AdSense Ads in A WebView in Android. I know
I would like to set up an error page which will diplay a message
I would like to display other we pages in my mobile webapp, but some
I would like to display the image of two real valued analytic functions f,g
I'm currently setting up DISQUS and would like to display the number of comments
I have the following problem: I have two models: Article and Comment, in Comments,

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.