Example – Like Dislike system with PHP, jQuery and Ajax

126

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.

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.

Database Design:

Create a database like tutspointer 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.

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.phpand 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.

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.

 

Leave A Reply

Your email address will not be published.