TABLE OF CONTENTS
DOWNLOAD & INSTALLATION
First, the download links for the module, and also “installation instructions”.
LICENSE & DOWNLOAD
Core Boxx is released under the MIT License. You are free to use it for personal and commercial projects, and modify it as you see fit. On the condition that the software is provided “as-is”. There are no warranties provided and “no strings attached”. Code Boxx and the authors are not liable for any claims, damages, or liabilities.
INSTALLATION
- Install the Users Module first.
- Copy/unzip this module into your existing Core Boxx project folder.
- Access
http://your-site.com/install/comments
, this will automatically:- Import
lib/SQL-Comments.sql
into your database. - Delete
PAGE-install-comments.php
itself.
- Import
- After installation, access
http://your-site.com/comment
for the demo page.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
FILES LIST
LIBRARIES
lib/LIB-Comments.php
The comments library.lib/SQL-Comments.sql
Comments database table.
API
lib/API-comments.php
Comments API endpoint.
ASSETS & PAGES
pages/PAGE-comment.php
Comments demo page.assets/PAGE-comment.js
Comments demo page Javascript.assets/comment.webp
Dummy product image.
DEVELOPMENT NOTES
- This module assumes that comments are only open for registered users.
- If you want to open up for the public, some form of identification needs still needs to be in place – Use a random cookie session ID as the “user ID”.
- Also, you decide if users can update and delete their own comments.
DATABASE REFERENCE
There is only one comment table. This should be enough in most cases, feel free to add more fields as required.
COMMENTS TABLE
Field | Description |
comment_id |
Primary key, auto-increment. |
user_id |
The user that made the comment. |
id |
The ID of whatever you want to attach comments to – Post, product, image, video, etc… |
timestamp |
The time when the comment is posted. |
message |
The comment itself. |
ADDING REPLIES
If you want to allow “reply to comment”:
- Add a
parent_id
field to the comments table. - Comments with
parent_id=0
is a “main comment”, comments withparent_id>0
is a reply to another comment. - Check out this post on categories and sub-categories on how to get all the comments recursively.
- Be warned though, the complexity can go out of control – It will be difficult to do pagination, you may want to limit the number of levels.
LIBRARY REFERENCE
Lastly, the list of library functions and API endpoints.
COMMENTS LIBRARY FUNCTIONS
Get all comments for the specified ID.
$id
Content ID – Post, page, image, video, product, whatever you attached a comment to.$page
Page number, optional.
$comments = $_CORE->Comments->getAll(999);
Get the specified comment.
$comment = $_CORE->Comments->get(123);
A helper function to check if a user has the right to edit/delete a comment. That is, a user can only edit/delete their own comments. Administrators have full rights to all.
// CHECK IF CURRENT USER HAS RIGHTS TO EDIT/DELETE COMMENT ID 123
echo $_CORE->Comments->check(123) ? "YES" : "NO" ;
Save a comment, no user access check.
$message
Comment message itself.$id
The post/product/video/image ID.$uid
User ID.$cid
Comment ID – If left blank, will add a new entry, if not, update this entry.
echo $_CORE->Comments->save(
"User 456 add comment to content 123.",
123, 456
) ? "OK" : $_CORE->error;
Save a comment, with user access check.
$message
Comment message itself.$id
The post/product/video/image ID.$cid
Comment ID – If left blank, will add a new entry, if not, update this entry.
echo $_CORE->Comments->savewc(
"Current user in $_SESSION["user"] add comment to content 123.",
123
) ? "OK" : $_CORE->error;
Deletes the specified comment ID. No user access check.
echo $_CORE->Comments->del(999) ? "OK" : $_CORE->error ;
Check user permission before deleting the comment.
echo $_CORE->Comments->delwc(123) ? "OK" : $_CORE->error ;
COMMENTS API FUNCTIONS
Save a comment.
$_POST["id"]
– INT, the post/product/video/image ID.$_POST["message"]
– STRING, comment.$_POST["cid"]
– INT, comment ID. Only if updating a comment.
Delete a comment.
$_POST["cid"]
– INT, the comment ID.
Get comments.
$_POST["id"]
– INT, “item ID”.