I was wondering if anyone had any good places I could look at to learn how to write a comments script. I need to allow comments on content if people are signed in. Thanks

Dani AI

Generated

Thread context: asked about threaded replies; pointed to tutorials and sketched a simple comments table. The following expands that discussion with a practical schema, retrieval patterns (SQL and application-side), and safety/UX notes for building threaded replies that render like Disqus.

A robust comments table (adds parent link, depth and a materialized path for stable ordering):

CREATE TABLE comments (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  item_id INT UNSIGNED NOT NULL,
  user_id INT UNSIGNED NOT NULL,
  parent_id INT UNSIGNED DEFAULT NULL,
  content TEXT NOT NULL,
  path VARCHAR(255) NOT NULL DEFAULT '',
  depth TINYINT UNSIGNED NOT NULL DEFAULT 0,
  status ENUM('approved','pending','deleted') NOT NULL DEFAULT 'approved',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX(item_id, parent_id),
  INDEX(item_id, path)
);

Retrieval options:

  • Modern SQL (MySQL 8+/Postgres): a recursive CTE can produce a depth-ordered thread and a stable path for sorting. Example pattern builds a string path of padded IDs and orders by it.
  • Older MySQL: select all comments for the item and build the tree in application code (map by id, attach children, then render roots). That avoids complex SQL and is fast if comments per item are reasonably bounded.

Rendering and behavior tips:

  • Store created_at as DATETIME (not varchar). Keep user_id as a foreign key; join when displaying names/avatars. Limit nesting (e.g., 4–6 levels) to keep layout readable. Use CSS (margin-left or nested ULs) to indent replies.
  • Security and moderation: use prepared statements, escape output (htmlspecialchars) or sanitize with a library if allowing HTML, implement a status column for moderation, and rate-limit/comment throttling to prevent abuse.
  • Performance: index (item_id, parent_id) for common queries, paginate top-level comments, and lazy-load deep reply branches via AJAX to reduce initial payload.

These pieces together cover the schema, query options, rendering strategy, and practical safety/UX considerations for threaded replies discussed earlier in the thread.

Recommended Answers

All 8 Replies

I would understand how to do the actual php coding, I'm just wondering if anyone can kind of explain the database table setup to me.

If ya want a tutorial all it takes is a google or yahoo search. A good one I found was at which shows how to make a comments box in a similar way that I would do it although there is a lot of optional code in the tutorial. Hope that helps and just let me know if you need a hand understanding the code.

Alright I kind of came up with my own solution. Basically store the user that's commenting's username and then store the content's id number. then just recall all the comments based on the id number. I appreciate the reply. Now I have to think up a way of doing replies.

Well for comments script u can visit php tutorials at

where u will get to know variety of ways through which u can comment in PHP

you can create two tables and then join them when u query for the data. one is the user table and the other is the comments table

im just posting the comments table. the other table u make urself.

--------------------------
comment_id
int(11)
NOT NULL
auto increment
----------------------
comment_content
text
NOT NULL
-----------------------
user
int(11)
NOT NULL
-------------------------
date_posted
varchar(50)
NOT NULL
------------------------

use user field to link with the user table.

yeah i got that up and running. any suggestions on how to do replies and have them show up in a thread fashion the way disqus does it? i.e. a reply to a comment would be indented from the rest of the comments.

anyone?

And so with your current script you can post comments but not view them? What exactly needs to be done? Have you already done any code in relation to this and if so please post it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.