2 years ago (2015-10-05 20:43:59)

Os: Facebook and YouTube style Like / Dislike system with PHP, MySQL + jQuery

10 4
2k

I wrote this note before but IT HELL flushed because of bad Internet, now i'm back with clean and smartly working codes. Youtube and Facebook codes are one of most wanted mechanisms on Internet i think but there not much heavy engineering into work, only few lines of codes boost up your show.

We'll create Like Dislike mechanism with three major Files. But First your Database must require following fields:

id title body likes dislikes

"id" is for recognizing the post to increment likes or dislikes.

Lets Create our Database first if you don't have any like that or have problem in creatin one.

Run these code blocks into your SQL Engine, if don't know that even the downloadable index.php file has same command that works automatically so no problem further.

/*------------------------ SQL code block starts here ----------------------------------*/

CREATE TABLE IF NOT EXISTS `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`body` text NOT NULL,
`likes` int(11) NOT NULL,
`dislikes` int(11) NOT NULL,
`comments` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `posts`
--

INSERT INTO `posts` (`id`, `title`, `body`, `likes`, `dislikes`, `comments`) VALUES
(1, 'Hello PHP', 'PHP is a fun language to learn and teach....', 3, 0, 0),
(2, 'Harry Potter series ended', 'Harry Potter is one of the most famous idol for kids....', 0, 2, 0);

/*------------------------ SQL code block ends here ----------------------------------*/

Now, Main PHP file where all the posts and Likes/Dislikes are going to appear:

index.php

/*------------------------ index code block starts here ----------------------------------*/

<?php

/* find more easy tutorials about PHP and Javacript at #CODEmgks
https://www.infeeds.com/topic/CODEmgks */

include_once('my_db.php'); // your db connection file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Like/Dislike Posts (Infeeds Notes at @mgksCODE)</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type="text/javascript" src="js/incremento.js"></script>

</title>

</head>

<body>

<?php

$sql = mysql_query('SELECT * FROM posts');

while($row = mysql_fetch_array($sql)){

echo '<div>

<h3>'.$row['title'].'</h3>

<p>'.$row['body'].'</p>

<a href="javascript:;" onClick="makeChange(\''.$row['id'].'\',\'like\');"><span id="'.$row['id'].'_likes">Like '.$row['likes'].'</span></a><br>

<a href="javascript:;" onClick="makeChange(\''.$row['id'].'\',\'dislike\');"><span id="'.$row['id'].'_dislikes">Dislike '.$row['dislikes'].'</span></a>

</div>';

}

?>

<br>

</body>

</html>

?>

/*------------------------ index code block ends here ----------------------------------*/

increment.php

/*------------------------ increment code block starts here ----------------------------------*/

<?php

include_once('my_db.php'); // your db connection file

if($_POST['id'] !='' && $_POST['type'] !=''){

if($_POST['type']=='like'){

mysql_query('UPDATE posts SET likes=likes+1 WHERE id="'.$_POST['id'].'"');

$num = mysql_fetch_array(mysql_query('SELECT likes FROM posts WHERE id="'.$_POST['id'].'"'));

echo 'Like '.$num['likes'].'';

}else if($_POST['type']=='dislike'){

mysql_query('UPDATE posts SET dislikes=dislikes+1 WHERE id="'.$_POST['id'].'"');

$num = mysql_fetch_array(mysql_query('SELECT dislikes FROM posts WHERE id="'.$_POST['id'].'"'));

echo 'Dislike '.$num['dislikes'].'';

}else{

echo 'error';

}

}

?>

/*------------------------ increment code block ends here ----------------------------------*/

incremento.js

/*------------------------ javascript code block starts here ----------------------------------*/

function makeChange(id,type){

$.post('increment.php', {id:id, type:type}, function(data){

$('#'+id+'_'+type+'s').text($.trim(data));

});

}

/*------------------------ javascript code block ends here ----------------------------------*/

If you have any Question tag this topic for that.

Download Complete Code for Facebook Youtube Like Dislike with PHP jQuery on Github

Sign up or Sign in for free.
And start a new thread.
an14555548443682
Very useful tutorial i also read another tutorial to create Youtube Style Like And Dislike Rating System Using jQuery,Ajax And PHP on talkerscode.com have a look with demo
http://talkerscode.com/webtricks/youtube-style-like-and-dislike-rating-system-using-jquery-ajax-and-php.php
mgks
nice.
2 years ago   0 Report Permalink[S]
HaveDicks
thanks man really helped in my project
4 years ago   1 Report Permalink[S]
ifreporter
/u/ifreporter(ifreporter.infeeds.com)
@HaveDicks nice name Mr. Dicks !!
3 years ago   0 Report Permalink[S]
More notes by Ghazi Khan