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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:56:54+00:00 2026-05-24T15:56:54+00:00

I have a system of comments. Each comment may receive replies, and each reply

  • 0

I have a system of comments. Each comment may receive replies, and each reply may receive replies, ad nauseam.

Thus, my database contains a table named “comments” with the following important fields:

id
text
reply_to

…under reply_to, of course, goes the id of the comment to which it is a reply.

So now, the problem is simple: how do I display all the comments, but knowing that after each comment must come the replies to it, and after each reply must come the replies to the reply, etc?

What I tried the most, and what I keep coming back at, is something like this:

$query = mysql_query("SELECT * FROM comments WHERE reply_to=0");
while ($comment = mysql_fetch_array($query))
    include("comment.php");

And in comment.php, what I have is:

foreach ($comment as $key = $value) $$key = $value;
echo $text;
echo "<div style='margin-left:30px;'>"; //A margin for a little indent
$subquery = mysql_query("SELECT * FROM comments WHERE reply_to=$id");
while ($comment = mysql_fetch_array($subquery))
    include("comment.php");
echo "</div>";

But if I reproduced correctly the essence of my code, the problem is this: that after the first reply is echoed, it goes on to the first reply of the first reply, and then the first reply of the first reply of the first reply, but the loop never gets to the second reply of anything. So for example, supposing the table had 3 comments, each of which had 3 replies, and each of which had 3 replies, etc, the above code would output:

Comment
  First reply
    First second-order reply
      First third-order reply
        ...

I hope I have explained it clearly enough. inb4: I cannot add new columns to the table.

  • 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-24T15:56:55+00:00Added an answer on May 24, 2026 at 3:56 pm

    In pseudocode:

    function display_comment(comm)
      echo comm's info and text
      children = get children of the comment: SELECT from comments WHERE parent = (comm's id)
      echo <div class="comment-thread">
      foreach children as child
        display_comment(comm) // notice this line
      echo </div>
    

    You must make a function, to make it recursively referenced.

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

Sidebar

Related Questions

I have a comment system written in PHP and JavaScript but each time I
I am making a nested comment system, and I have the following table: create
I'm working on a case logging system. Each case can have comments attached, so
I have a system where registered users can vote up/vote down comments for a
I'm working on a social networking system that will have comments coming from several
I have been making a threaded comment system as a way to learn php
Let's say we have system A comprising a MySQL database, with several tables. After
We have a rules processing system where each rule is represented by a class
I'm part-way through making a simple comments system with PHP. With every comment that
I am creating a Comment-Reply system, similar to stackoverflow and I wonder how to

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.