Multi-tiered Comment Replies: Display and Storage

前端 未结 5 1871
夕颜
夕颜 2021-02-02 04:18

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

5条回答
  •  误落风尘
    2021-02-02 05:01

    In database, you may create a table with foreign key column (parent_comment), which references to comments table itself. For example:

    CREATE TABLE comments (
      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      parent_comment INT FOREIGN KEY REFERENCES comments(id),
      date_posted  DATETIME,
      ...)
    

    In order to show comments to single item, you'll have to select all comments for particular item, and parse them recursively in your script with depth-first algorithm. Chronological order should be taken into account in traversal algorithm.

提交回复
热议问题