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

The Archive Base Latest Questions

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

I’m trying to make a commenting thread like Disqus has done. I’m having trouble

  • 0

I’m trying to make a commenting thread like Disqus has done. I’m having trouble setting up the logic in PHP to display the comments such that one can reply to each comment and those comments will then stay attached to each other.

here’s my MySQL comments table:

comment_id  comment  item_id  replied_to_id  time
 1           hello1    1          1         1:00pm
 2           hello2    1          1         2:00pm
 3           hello3    1          3         3:00pm
 4           hello4    2          4         4:00pm
 5           hello5    1          2         2:00pm
 6           hello6    1          3         6:00pm

item_id is a column which indicates the parent item that the comments are discussing.

If I pull from my database all the comments with item_id=1, then I’m not sure how to thread them such that the comment_id and replied_to_id‘s are appropriately matched. For example, comment_id=2 should be matched to comment_id=1.

Here’s my PHP:

<?foreach($comments as $row){
  if($row->comment_id==$row->replied_to_id){
    echo $row->comment."-".$row->time; //desired output: hello1-1:00pm    
      foreach($comments as $sub_row){
        if($row->comment_id!=$sub_row->replied_to_id){
           echo $sub_row->comment."-".$sub_row->time;// desired output: hello2-2:00pm
             foreach($comments as $sub_sub_row){
                if($row->comment_id!=$sub_sub_row->replied_to_id){
                  echo $sub_sub_row->comment."-".$sub_sub_row->time;// desired output: hello5-5:00pm  
                }
             } 
        }
     }          
  }
  else{
    echo $row->comment."-".$row->time;   // desired output: hello3-3:00pm
  }
}

This just looks wrong. There has got to be a better way to do this.

  • 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:10:35+00:00Added an answer on June 3, 2026 at 1:10 am

    Simple demo, not necessarily the most optimal, but working:

    function displayComments(array $comments, $parentId = null) {
        foreach ($comments as $comment) {
            if ($comment['replied_to_id'] == $parentId) {
                echo $comment['comment'];
                displayComments($comments, $comment['id']);
            }
        }
    }
    

    This assumes top-level comments have no replied_to_id (null). Your example with comment 1 replying to 1 doesn’t make much sense.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.