Background
When developing websites for my clients, I'm usually asked to add a simple comments form, where users can post and view comments. In these situations, I'm tempted to use third party commenting system such as Disqus, but often the clients want their own integrated solution. For this reason, I've developed a simple comments class that can be reused in other projects and in this tutorial I'll be showing you how to create your own.Implementation
The chosen programming language for this tutorial is PHP and we'll be using MySql as our database. The first thing we need to do is create a database table that can store the comments. Listing 1.1 below shows a create table script.Listing 1.1
- CREATE TABLE comments (
- id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
- uid VARCHAR(45) DEFAULT NULL,
- full_name VARCHAR(45) DEFAULT NULL,
- comment text,
- created_date_time datetime DEFAULT NULL,
- PRIMARY KEY (id)
- ) ENGINE=MyISAM
Now that the database table has been created, it's time to start some coding. We are going to create a simple PHP class, that we can use to add, query and delete comments. I'm going to be using PHP's PDO class, if your not familiar with PDO, I recommend, visiting the PHP PDO documentation before you continue here.
The Code
Open your editor of choice and create a new PHP file. The first thing, we're going to do is create a class called CommentManager. This class will allow us to add,get,delete comments. It will also initiate a database connection. Let's start with the initialization of the database connection. Copy the code from listing 1.2 below.Listing 1.2
- class CommentManager {
- private $pdo = null;
- public function __construct($host, $dbname, $username, $password){
- $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
- $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- }
Now we need a method to add a comment. Copy the code in listing 1.3 below into your CommentManager class.
Listing 1.3
- public function addComment($fullName, $comment, $uid = null){
- if ($this->pdo){
- $stm = $this->pdo->prepare("INSERT INTO comments (full_name,comment,uid,created_date_time) VALUES (:full_name,:comment,:uid,NOW())");
- $stm->bindParam(':full_name', $fullName);
- $stm->bindParam(':comment', $comment);
- $stm->bindParam(':uid', $uid);
- if ($stm->rowCount()){
- return true;
- }
- }
- return false;
- }
Now that we can add a comment, we need a way to return all comments. Copy the code in listing 1.4 below into your class.
Listing 1.4
- public function getComments($uid=null){
- if ($this->pdo){
- $sql = 'SELECT * FROM comments ';
- if($uid){
- $sql.=' WHERE uid=:uid ORDER BY created_date_time';
- }
- $stm = $this->pdo->prepare($sql);
- if($uid){
- $stm->bindParam(':uid', $uid);
- }
- if ($stm->execute()){
- return $stm->fetchAll(PDO::FETCH_OBJ);
- }
- }
- return false;
- }
Finally we need a delete method, for admin purposes. Listing 1.5 below shows the complete code.
Listing 1.5
- class CommentManager {
- private $pdo = null;
- public function __construct($host, $dbname, $username, $password){
- $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
- $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- public function addComment($fullName, $comment, $uid = null){
- if ($this->pdo){
- $stm = $this->pdo->prepare("INSERT INTO comments (full_name,comment,uid,created_date_time) VALUES (:full_name,:comment,:uid,NOW())");
- $stm->bindParam(':full_name', $fullName);
- $stm->bindParam(':comment', $comment);
- $stm->bindParam(':uid', $uid);
- if ($stm->execute()){
- return true;
- }
- }
- return false;
- }
- public function getComments($uid=null){
- if ($this->pdo){
- $sql = 'SELECT * FROM comments ';
- if($uid){
- $sql.=' WHERE uid=:uid ORDER BY created_date_time';
- }
- $stm = $this->pdo->prepare($sql);
- if($uid){
- $stm->bindParam(':uid', $uid);
- }
- if ($stm->execute()){
- return $stm->fetchAll(PDO::FETCH_OBJ);
- }
- }
- return false;
- }
- public function deleteCommentById($id){
- if ($this->pdo){
- $stm = $this->pdo->prepare("DELETE FROM comments WHERE id=:id");
- $stm->bindParam(':id', $id);
- if ($stm->execute()){
- return true;
- }
- }
- return false;
- }
- }
Listing 1.6
- try{
- $cm = new CommentManager('localhost', 'dbname', 'root', '');
- // Add a new comment
- $cm->addComment('John Doe', 'This is my first post','uid123456');
- // Get all comments
- $comments = $cm->getComments();
- // Get all comments by uid
- $comments = $cm->getComments('uid123456');
- // Delete comment by comment id
- $cm->deleteCommentById(1);
- }catch(PDOException $e){
- // Print error message
- echo $e->getMessage();
- }
The final part of the coding requires a simple form to allow a user to post comments and a comments view to read all comments but that wont be covered in this tutorial. This brings me to the end of this tutorial. Please leave any comments that can help improve this tutorial.
No comments:
Post a Comment