PHP Toggle Like Dislike Rating System using jQuery Ajax

PHP Toggle Like Dislike Rating System using jQuery Ajax

Today, We want to share with you PHP Toggle Like Dislike Rating System using jQuery Ajax.
In this post we will show you Like Unlike using PHP jQuery, hear for How to Create Like & Unlike code like FB uisng PHP, MySQL and jQuery we will give you demo and example for implement.
In this post, we will learn about Like Dislike rating system with jQuery, Ajax and PHP with an example.

PHP Toggle Like Dislike Rating System using jQuery Ajax

There are the Following The simple About PHP Toggle Like Dislike Rating System using jQuery Ajax Full Information With Example and source code.

As I will cover this Post with live Working example to develop Like and Dislike System with jQuery, PHP and MySQL, so the some major files and Directory structures for this example is following below.

  • index.php
  • review.php
  • Poll.php
  • review.js
  VueJS nested child, template, inner template, components

Step 1: Make MySQL Table

Make MySQL database table simple All the projects to store projects some records

CREATE TABLE `projects` (
`project_id` double NOT NULL,
`visitor_id` double DEFAULT NULL,
`message` text,
`date` double DEFAULT NULL,
`review_up` int(11) DEFAULT '0',
`review_down` int(11) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

project_reviews

CREATE TABLE `project_reviews` (
`id` int(11) NOT NULL,
`project_id` double DEFAULT NULL,
`visitor_id` double DEFAULT NULL,
`review` tinyint(1) DEFAULT NULL,
`date` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Example of The PHP Toggle Like Dislike Rating System

Step 2: Listing Records with Like and Dislike

Showing All the Records with Reviews (Like and Dislike) Count
index.php

This is where I will make a simple HTML form and PHP server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as index.php.

<div class="row" id="project-list">
<?php
include ('Projects.php');
$projects = new Projects();
$projectsData = $projects->getProjects();
foreach($projectsData as $project) {
?>
<div class="col-sm-4 col-lg-4 col-md-4 project-body">
<div class="project-content"><?php echo $project['message']; ?></div>
<div class="project-options pull-right">
<a class="options" data-review-type="1" id="project_review_up_<?php echo $project['project_id']; ?>"><i class="glyphicon glyphicon-thumbs-up" data-original-title="Like this project"></i></a>
<span class="counter" id="review_up_count_<?php echo $project['project_id']; ?>">  <?php echo $project['review_up']; ?></span>   
<a class="options" data-review-type="0" id="project_review_down_<?php echo $project['project_id']; ?>"><i class="glyphicon glyphicon-thumbs-down" data-original-title="Dislike this project"></i></a>
<span class="counter" id="review_down_count_<?php echo $project['project_id']; ?>">  <?php echo $project['review_down']; ?></span>
</div>
</div>
<?php } ?>
</div>

Step 3: Implement Post Like and Dislike

review.js

$(document).ready(function() {
$(".options").on("click", function(){
var project_id = $(this).attr("id");
project_id = project_id.replace(/\D/g,'');
var review_type = $(this).data("review-type");
$.ajax({
type : 'POST',
url : 'review.php',
dataType:'json',
data : {project_id:project_id, review_type:review_type},
success : function(response){
$("#review_up_count_"+response.project_id).html("  "+response.review_up);
$("#review_down_count_"+response.project_id).html("  "+response.review_down);
}
});
});
});

Step 4: like-and-dislike-system

review.php

<?php
include ('Projects.php');
$projects = new Projects();
// get logged in userid to review
$visitor_id = 123456;
if($_POST['project_id'] && $visitor_id) {
$projectReview = $projects->getPostReviews($_POST['project_id']);
$userReview = $projects->getUserReviews($visitor_id, $_POST['project_id']);
if($_POST['review_type'] == 1) {
if($projects->isUserAlreadyReviewd($visitor_id, $_POST['project_id']) && !$userReview['review']) {
$projectReview['review_up'] += 1;
$projectReview['review_down'] -= 1;
$userReview['review'] = 1;
} else if (!$projects->isUserAlreadyReviewd($visitor_id, $_POST['project_id'])){
$projectReview['review_up'] += 1;
$userReview['review'] = 1;
}
} else if($_POST['review_type'] == 0) {
if($projects->isUserAlreadyReviewd($visitor_id, $_POST['project_id']) && $userReview['review']) {
$projectReview['review_up'] -= 1;
$projectReview['review_down'] += 1;
$userReview['review'] = 0;
} else if(!$projects->isUserAlreadyReviewd($visitor_id, $_POST['project_id'])) {
$projectReview['review_down'] += 1;
$userReview['review'] = 0;
}
}
$projectReviewData = array(
'project_id' => $_POST['project_id'],
'visitor_id' => $visitor_id,
'review_up' => $projectReview['review_up'],
'review_down' => $projectReview['review_down'],
'user_review' => $userReview['review']
);
// update project reviews
$projectReviewd = $projects->updatePostReview($projectReviewData);
if($projectReviewd) {
$response = array(
'review_up' => $projectReview['review_up'],
'review_down' => $projectReview['review_down'],
'project_id' => $_POST['project_id']
);
echo json_encode($response);
}
}
?>

Step 5: Class with Methods

Projects.php

<?php
class Projects{
private $host = 'localhost';
private $user = 'root';
private $password = '[email protected]';
private $database = 'pakainfo';
private $projectTable = 'projects';
private $projectReviewsTable = 'project_reviews';
private $dbConnect = false;
public function __construct(){
if(!$this->dbConnect){
$conn = new mysqli($this->host, $this->user, $this->password, $this->database);
if($conn->connect_error){
die("Error failed to connect to MySQL: " . $conn->connect_error);
}else{
$this->dbConnect = $conn;
}
}
}
private function executeQuery($sqlLiveQQ) {
$response = mysqli_query($this->dbConnect, $sqlLiveQQ);
if(!$response){
die('Error in query: '. mysqli_error());
}
$data= array();
while ($row = mysqli_fetch_array($response, MYSQL_ASSOC)) {
$data[]=$row;
}
return $data;
}
public function getProjects(){
$sqlLiveQQ = 'SELECT project_id, visitor_id, message, date, review_up, review_down FROM '.$this->projectTable;
return $this->executeQuery($sqlLiveQQ);
}
public function isUserAlreadyReviewd($visitor_id, $project_id){
$sqlLiveQQ = 'SELECT project_id, visitor_id, review FROM '.$this->projectReviewsTable." WHERE visitor_id = '".$visitor_id."' AND project_id = '".$project_id."'";
$response = mysqli_query($this->dbConnect, $sqlLiveQQ);
return mysqli_num_rows($response);
}
public function getUserReviews($visitor_id, $project_id){
$sqlLiveQQ = 'SELECT project_id, visitor_id, review FROM '.$this->projectReviewsTable." WHERE visitor_id = '".$visitor_id."' AND project_id = '".$project_id."'";
$response = mysqli_query($this->dbConnect, $sqlLiveQQ);
return mysqli_fetch_array($response, MYSQL_ASSOC);
}
public function getPostReviews($project_id){
$sqlLiveQQ = 'SELECT project_id, visitor_id, review_up, review_down FROM '.$this->projectTable." WHERE project_id = '".$project_id."'";
$response = mysqli_query($this->dbConnect, $sqlLiveQQ);
return mysqli_fetch_array($response, MYSQL_ASSOC);
}
public function updatePostReview($projectReviewData) {
$sqlLiveQQ = "UPDATE ".$this->projectTable." SET review_up = '".$projectReviewData['review_up']."' , review_down = '".$projectReviewData['review_down']."' WHERE project_id = '".$projectReviewData['project_id']."'";
mysqli_query($this->dbConnect, $sqlLiveQQ);
$sqlReviewQuery = '';
if($this->isUserAlreadyReviewd($projectReviewData['visitor_id'], $projectReviewData['project_id'])) {
$sqlReviewQuery = "UPDATE ".$this->projectReviewsTable." SET review = '".$projectReviewData['user_review']."' WHERE project_id = '".$projectReviewData['project_id']."' AND visitor_id = '".$projectReviewData['visitor_id']."'";
} else {
$sqlReviewQuery = "INSERT INTO ".$this->projectReviewsTable." (id, project_id, visitor_id, review) VALUES ('', '".$projectReviewData['project_id']."', '".$projectReviewData['visitor_id']."', '".$projectReviewData['user_review']."')";
}
if($sqlReviewQuery) {
mysqli_query($this->dbConnect, $sqlReviewQuery);
return true;
}
}
}
?>

PHP Toggle Like Dislike Rating System – Output

PHP MySQLi Toggle Like Dislike Rating System - PHP Toggle Like Dislike Rating System using jQuery Ajax
PHP MySQLi Toggle Like Dislike Rating System
Angular 6 CRUD Operations Application Tutorials

Read :

  Bootstrap get active tabs index using jQuery Example

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How To Toggle Like and Dislike using PHP MySQLi.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

avatar
  Subscribe  
Notify of