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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:54:37+00:00 2026-05-17T21:54:37+00:00

So I’m trying to create a comment system in which you can reply to

  • 0

So I’m trying to create a comment system in which you can reply to comments that are already replies (allowing you to create theoretically infinite threads of replies). I want them to display in chronological order (newest on top), but of course the replies should be directly underneath the original comment. If there are multiple comments replying to the same comment, the replies should also be in chronological order (still underneath the original comment). I also want to limit the number of comment groups (a set of comments with a single comment that is not a reply at all) to, say, 25. How should I set up the MySQL table, and what sort of query would I use to extract what I want?

Here’s a simplified version of my DB:
ID int(11) NOT NULL AUTO_INCREMENT,
DatePosted datetime NOT NULL,
InReplyTo int(11) NOT NULL DEFAULT ‘0’,

Sorry if this is kind of confusing, I’m not sure how to word it any differently. I’ve had this problem in the back of my mind for a couple months now, and every time I solve one problem, I end up with another…

  • 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-17T21:54:37+00:00Added an answer on May 17, 2026 at 9:54 pm

    There are many ways. Here’s one approach that I like (and use on a regular basis).

    The database

    Consider the following database structure:

    CREATE TABLE comments (
      id int(11) unsigned NOT NULL auto_increment,
      parent_id int(11) unsigned default NULL,
      parent_path varchar(255) NOT NULL,
    
      comment_text varchar(255) NOT NULL,
      date_posted datetime NOT NULL,  
    
      PRIMARY KEY  (id)
    );
    

    your data will look like this:

    +-----+-------------------------------------+--------------------------+---------------+
    | id  | parent_id | parent_path             | comment_text             | date_posted   |
    +-----+-------------------------------------+--------------------------+---------------+
    |   1 | null      | /                       | I'm first                | 1288464193    | 
    |   2 | 1         | /1/                     | 1st Reply to I'm First   | 1288464463    | 
    |   3 | null      | /                       | Well I'm next            | 1288464331    | 
    |   4 | null      | /                       | Oh yeah, well I'm 3rd    | 1288464361    | 
    |   5 | 3         | /3/                     | reply to I'm next        | 1288464566    | 
    |   6 | 2         | /1/2/                   | this is a 2nd level reply| 1288464193    | 
    
    ... and so on...
    

    It’s fairly easy to select everything in a useable way:

    select id, parent_path, parent_id, comment_text, date_posted
    from comments 
    order by parent_path, date_posted;
    

    ordering by parent_path, date_posted will usually produce results in the order you’ll need them when you generate your page; but you’ll want to be sure that you have an index on the comments table that’ll properly support this — otherwise the query works, but it’s really, really inefficient:

    create index comments_hier_idx on comments (parent_path, date_posted);
    

    For any given single comment, it’s easy to get that comment’s entire tree of child-comments. Just add a where clause:

    select id, parent_path, parent_id, comment_text, date_posted
    from comments 
    where parent_path like '/1/%'
    order by parent_path, date_posted;
    

    the added where clause will make use of the same index we already defined, so we’re good to go.

    Notice that we haven’t used the parent_id yet. In fact, it’s not strictly necessary. But I include it because it allows us to define a traditional foreign key to enforce referential integrity and to implement cascading deletes and updates if we want to. Foreign key constraints and cascading rules are only available in INNODB tables:

    ALTER TABLE comments ENGINE=InnoDB;
    
    ALTER TABLE comments 
      ADD FOREIGN KEY ( parent_id ) REFERENCES comments 
        ON DELETE CASCADE 
        ON UPDATE CASCADE;
    

    Managing The Hierarchy

    In order to use this approach, of course, you’ll have to make sure you set the parent_path properly when you insert each comment. And if you move comments around (which would admittedly be a strange usecase), you’ll have to make sure you manually update each parent_path of each comment that is subordinate to the moved comment. … but those are both fairly easy things to keep up with.

    If you really want to get fancy (and if your db supports it), you can write triggers to manage the parent_path transparently — I’ll leave this an exercise for the reader, but the basic idea is that insert and update triggers would fire before a new insert is committed. they would walk up the tree (using the parent_id foreign key relationship), and rebuild the value of the parent_path accordingly.

    It’s even possible to break the parent_path out into a separate table that is managed entirely by triggers on the comments table, with a few views or stored procedures to implement the various queries you need. Thus completely isolating your middle-tier code from the need to know or care about the mechanics of storing the hierarchy info.

    Of course, none of the fancy stuff is required by any means — it’s usually quite sufficient to just drop the parent_path into the table, and write some code in your middle-tier to ensure that it gets managed properly along with all the other fields you already have to manage.


    Imposing limits

    MySQL (and some other databases) allows you to select “pages” of data using the the LIMIT clause:

    SELECT * FROM mytable LIMIT 25 OFFSET 0;
    

    Unfortunately, when dealing with hierarchical data like this, the LIMIT clause alone won’t yield the desired results.

    -- the following will NOT work as intended
    
    select id, parent_path, parent_id, comment_text, date_posted
    from comments 
    order by parent_path, date_posted
    LIMIT 25 OFFSET 0;
    

    Instead, we need to so a separate select at the level where we want to impose the limit, then we join that back together with our “sub-tree” query to give the final desired results.

    Something like this:

    select 
      a.*
    from 
      comments a join 
      (select id, parent_path 
        from comments 
        where parent_id is null
      order by parent_path, post_date DESC 
      limit 25 offset 0) roots
      on a.parent_path like concat(roots.parent_path,roots.id,'/%') or a.id=roots.id)
    order by a.parent_path , post_date DESC;
    

    Notice the statement limit 25 offset 0, buried in the middle of the inner select. This statement will retrieve the most recent 25 “root-level” comments.

    [edit: you may find that you have to play with stuff a bit to get the ability to order and/or limit things exactly the way you like. this may include adding information within the hierarchy that’s encoded in parent_path. for example: instead of /{id}/{id2}/{id3}/, you might decide to include the post_date as part of the parent_path: /{id}:{post_date}/{id2}:{post_date2}/{id3}:{post_date3}/. This would make it very easy to get the order and hierarchy you want, at the expense of having to populate the field up-front, and manage it as the data changes]

    hope this helps.
    good luck!

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

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.