Youtube Like & Dislike System using PHP, MySql, jQuery and Ajax

Youtube Like & Dislike System using PHP, MySql, jQuery and Ajax

Mar 14, 2018 | Javascript, MySql, PHP

If you tryed the demo, things will be much easier so we start by creating a table called  bi_yt_rate , in the tuto i created under a database called db

CREATE TABLE IF NOT EXISTS `wcd_yt_rate` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `id_item` INT(11) NOT NULL,
  `ip` VARCHAR(25) NOT NULL,
  `rate` INT(1) NOT NULL,
  `dt_rated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
);
Using jQuery, and on click on the button we change the look of the button by adding a class like and we send the parameters we need to the ajax file
(the example of code here is used for the like action)
<script>
    $(document).ready(function() {
        var pageID = <?php echo $pageID;  ?>;

        $('.like-btn').click(function(){
            $('.dislike-btn').removeClass('dislike-h');   
            $(this).addClass('like-h');
            $.ajax({
                type:"POST",
                url:"ajax.php",
                data:'act=like&pageID='+pageID,
                success: function(data){
                    $('#cartop'+pageID).html(data);
                }
            });
        });
        $('.dislike-btn').click(function(){
            $('.like-btn').removeClass('like-h');
            $(this).addClass('dislike-h');
            $.ajax({
                type:"POST",
                url:"ajax.php",
                data:'act=dislike&pageID='+pageID,
                success: function(data){
                    $('#cartop'+pageID).html(data);
                }
            });
        });
        $('.share-btn').click(function(){
            $('.share-cnt').toggle();
        });
    });
</script>

 

Now in the ajax.php file, we call the config.php file which we connect to the database with and then we check by IP if the user has already make a rate by counting the like and dislike of the pageID and his IP.
<?php
    include 'config.php';
    extract($_POST);
    $user_ip = $_SERVER['REMOTE_ADDR'];


    // check if the user has already clicked on the unlike (rate = 2) or the like (rate = 1)
        $dislike_sql = mysql_query('SELECT COUNT(*) FROM  bi_yt_rate WHERE ip = "'.$user_ip.'" and id_item = "'.$pageID.'" and rate = 2 ');
        $dislike_count = mysql_result($dislike_sql, 0);

        $like_sql = mysql_query('SELECT COUNT(*) FROM  bi_yt_rate WHERE ip = "'.$user_ip.'" and id_item = "'.$pageID.'" and rate = 1 ');
        $like_count = mysql_result($like_sql, 0);

    if($act == 'like'): //if the user click on "like"
        if(($like_count == 0) && ($dislike_count == 0)){
            mysql_query('INSERT INTO bi_yt_rate (id_item, ip, rate )VALUES("'.$pageID.'", "'.$user_ip.'", "1")');
           
        }
        if($dislike_count == 1){
            mysql_query('UPDATE bi_yt_rate SET rate = 1 WHERE id_item = '.$pageID.' and ip ="'.$user_ip.'"');
           
        }
       
            // count all the rate
        $rate_all_count = mysql_query('SELECT COUNT(*) FROM  bi_yt_rate WHERE id_item = "'.$pageID.'"');
        $rate_all_count = mysql_result($rate_all_count, 0);
       
        $rate_like_count = mysql_query('SELECT COUNT(*) FROM  bi_yt_rate WHERE id_item = "'.$pageID.'" and rate = 1');
        $rate_like_count = mysql_result($rate_like_count, 0); 
        $rate_like_percent = percent($rate_like_count, $rate_all_count);

        $rate_dislike_count = mysql_query('SELECT COUNT(*) FROM  bi_yt_rate WHERE id_item = "'.$pageID.'" and rate = 2');
        $rate_dislike_count = mysql_result($rate_dislike_count, 0); 
        $rate_dislike_percent = percent($rate_dislike_count, $rate_all_count);
?>
            <div class="rate-count" style="margin-top: -15px;"><?php echo $rate_all_count; ?></div>
                <div class="stat-bar">
                    <div class="bg-green" style="width:{{{PHP3}}}%;"></div>
                    <div class="bg-red" style="width:{{{PHP4}}}%"></div>
                </div><!-- stat-bar -->
                <div class="dislike-count"><?php echo $rate_dislike_count; ?></div>
            <div class="like-count"><?php echo $rate_like_count; ?></div>
<?php
    endif;
    if($act == 'dislike'): //if the user click on "like"
        if(($like_count == 0) && ($dislike_count == 0)){
            mysql_query('INSERT INTO bi_yt_rate (id_item, ip, rate )VALUES("'.$pageID.'", "'.$user_ip.'", "2")');
           
        }
        if($like_count == 1){
            mysql_query('UPDATE bi_yt_rate SET rate = 2 WHERE id_item = '.$pageID.' and ip ="'.$user_ip.'"');
           
        }
            // count all the rate
        $rate_all_count = mysql_query('SELECT COUNT(*) FROM  bi_yt_rate WHERE id_item = "'.$pageID.'"');
        $rate_all_count = mysql_result($rate_all_count, 0);
       
        $rate_like_count = mysql_query('SELECT COUNT(*) FROM  bi_yt_rate WHERE id_item = "'.$pageID.'" and rate = 1');
        $rate_like_count = mysql_result($rate_like_count, 0); 
        $rate_like_percent = percent($rate_like_count, $rate_all_count);

        $rate_dislike_count = mysql_query('SELECT COUNT(*) FROM  bi_yt_rate WHERE id_item = "'.$pageID.'" and rate = 2');
        $rate_dislike_count = mysql_result($rate_dislike_count, 0); 
        $rate_dislike_percent = percent($rate_dislike_count, $rate_all_count);

?>
            <div class="rate-count" style="margin-top: -15px;"><?php echo $rate_all_count; ?></div>
                <div class="stat-bar">
                    <div class="bg-green" style="width:{{{PHP9}}}%;"></div>
                    <div class="bg-red" style="width:{{{PHP10}}}%"></div>
                </div><!-- stat-bar -->
                <div class="dislike-count"><?php echo $rate_dislike_count; ?></div>
            <div class="like-count"><?php echo $rate_like_count; ?></div>
<?php
    endif;
?>

 

Being Idea is a web platform of programming tutorials to make better programming skills and provides Software Development Solutions.

1 Comment

  1. Anonymous

    but this for a page not only for a section

    Reply

Leave a Reply