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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:28:11+00:00 2026-06-09T03:28:11+00:00

I searched through the forums but couldn’t get an authoritative answer. I want to

  • 0

I searched through the forums but couldn’t get an authoritative answer. I want to implement a nested comment structure in a way like this:

<ul>
    <li>This is the parent first comment!
        <ul>
            <li>This is the reply for the first parent comment!
                <ul>
                    <li>This is a reply for the first reply of the parent comment!</li>
                    <li>This is a third reply for the first parent comment!</li>
                </ul>
            </li>
            <li>This is another reply for first parent comment!</li>
        </ul>
    </li>
    <li>This is gonna be parent second comment!
        <ul>
            <li>This is a reply for the second comment!</li>
        </ul>
    </li>
    <li>This is fourth parent comment!</li>
</ul>

The dump for my table is below:

+----+------------------------------------------------------------+--------+
| id | text                                                       | parent |
+----+------------------------------------------------------------+--------+
|  1 | This is the parent first comment!                          |      0 |
|  2 | This is gonna be parent second comment!                    |      0 |
|  3 | This is the reply for the first parent comment!            |      1 |
|  4 | This is another reply for first parent comment!            |      1 |
|  5 | This is a reply for the first reply of the parent comment! |      3 |
|  6 | This is a reply for the second comment!                    |      2 |
|  7 | This is a third reply for the first parent comment!        |      3 |
|  8 | This is fourth parent comment!                             |      0 |
+----+------------------------------------------------------------+--------+

I know how to use mysql_query() and while() loops. A beginner in PHP & MySQL. Please help me out.

  • 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-09T03:28:13+00:00Added an answer on June 9, 2026 at 3:28 am

    I had done something similar for my blogpost. Yet, I just tried out with the same data. When you say nested comments it is better you can use nested functions this way:

    function getComments($parent)
    {
        # Get the data from SQL.
        # Check if it has nested comments.
        if (hasComments())
            getComments($child); # $child is the ID of the current comment.
    }
    

    To make it this way, I have imported your data to MySQL and tried this:

    <?php
        mysql_connect('localhost', 'root');
        mysql_select_db('nestedcomments');
        function getComments($parent)
        {
            $res = mysql_query("SELECT * FROM `nestcomm` WHERE `parent` = $parent");
            if (mysql_num_rows($res))
            {
                echo "<ul>\n";
                while (($dat = mysql_fetch_array($res)) !== false)
                    echo "<li>", $dat["text"], getComments($dat["id"]), "</li>\n";
                echo "</ul>\n";
            }
            else
                echo ($parent === 0) ? 'No Comments!' : "";
        }
        getComments(0);
    ?>
    

    As I said before I have used nested functions, and as you asked the output is almost same (without the braces) this way:

    <ul>
    <li>This is the parent first comment!<ul>
    <li>This is the reply for the first parent comment!<ul>
    <li>This is a reply for the first reply of the parent comment!</li>
    <li>This is a third reply for the first parent comment!</li>
    </ul>
    </li>
    <li>This is another reply for first parent comment!</li>
    </ul>
    </li>
    <li>This is gonna be parent second comment!<ul>
    <li>This is a reply for the second comment!</li>
    </ul>
    </li>
    <li>This is fourth parent comment!</li>
    </ul>
    

    Hope this helps out.

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

Sidebar

Related Questions

I've asked Google and searched through the NServiceBus website and forums, but I can't
I have searched through the google and also joomla forums but didn't got what
I realise this may be a duplicate, but I have searched through many forums
I have searched through the forum for my answer, But all the calendar questions
i searched through Stack overflow but fail to find a solution in regards to
I have searched through many times but have not seen this before. Probably really
I've searched through the other expected Class Name error questions on here, but they
I've searched through the documentation and searched around but there is nothing said about
I'm pretty unexperienced with Action Script but searched the forums extensively to try and
I searched through a lot of questions on SO but I can't find the

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.