Like Dislike Rating System using PHP – Rating system is very useful for every web project. Through this system, web admin can track the visitors like and dislike. Also it will help webmaster to understand the visitors choices and make their website more interactive with the visitors.

We have made the simple rating system using PHP, MySQL, jQuery and Ajax. This rating system take the visitors like or dislike with jQuery and Ajax. Also store the data at the database using PHP and MySQL. Once the total number of likes and dislikes is calculated, it will be displayed to the visitors. We have made the whole process very easy and you can implement the rating system at your project very easily.

This tutorial will discuss about like dislike system in PHP. We have used OOP concept in PHP for make the whole script lightweight, simple and easy.

Like Dislike Rating System using PHP
Let’s start our tutorial. In this tutorial we will listing some tutorials with like and dislike icon. When like and dislike icon is clicked, like and dislike counter of that particular tutorial will be displayed beside those icons.

Like Dislike Rating System using PHP

Create a database like vetbossel and create a table into this database named tutorials. tutorials table creation SQL will be like below.

For testing purpose insert some data at the tutorials table. The respective images will be stored into the images/ directory at the root folder.

Then add the tutorial.php
We have used PHP OOP concept for fetch, insert and update data from the database. So, at first we will create a file named tutorial.php and define Tutorial class in this file.
__constructor() method will connect with MySQL database and select tutorials table.

get_rows() function fetch the tutorial rows from the tutorials table. By default get_rows() function return all rows, but if a particular tutorial ID is provided, then it will be returned only that particular row.

update() function is used for update the tutorials data.

index.php
At the beginning of the file, we will include tutorial.php file and create an instance of Tutorial class. Call the get_rows() function and stored all tutorials data into $trows variable.

cwRating() is a JavaScript function and we will used this function into onClick event of like or dislike icon. Also passing the tutorial ID in first parameter, rating type(1=Like, 0=Dislike) in the second parameter and counter element ID in the third parameter.

Demo
rating.php
Once the like or dislike icon is clicked, rating.php file is called. In this file we will fetch the previous numbers of likes or dislikes and increment the likes or dislikes numbers and returns the present numbers of likes or dislikes.

Click to download
Click to download

In this tutorial, we’ve provided the script for implement rating system using PHP, MySQL, jQuery and Ajax in your project. As per this script a user can rate multiple times for the same item. If you want to restrict users from multiple times rating, you can do it by two ways.

1.Allow the user to vote after login. Here you can easily track User Id and allow the user to vote only once.
2.Track the user IP address using $_SERVER[‘REMOTE_ADDR’] and allow the user to vote only once.