How To Create Facebook Style – Like Unlike Using PHP

If you are looking for a source code on How To Create Facebook Style – Like Unlike Using PHP then you are at the right place. This sample work is very familiar to us. Today, most of us are using Facebook, and the interesting part in Facebook is to like the post of your friend, family, and etc. In this article, we create a simple like unlike using PHP. For updating likes count in our database, we used jQuery. And, you can be used this source code in your simple SNS or it's called a Social Networking Site. HTML Like Unlike Form This source code where you can like and unlike the posts.
  1. <table class="demo-table">
  2.         <tbody>
  3.                 <tr>
  4.                 <th><strong>Sourcecodes In Sourcecodester</strong></th>
  5.                 </tr>
  6.         <?php
  7.         if(!empty($result)) {
  8.         $ip_address = $_SERVER['REMOTE_ADDR'];
  9.         foreach ($result as $tutorial) {
  10.         ?>
  11.                 <tr>
  12.                         <td valign="top">
  13.                         <div class="feed_title"><?php echo $tutorial["title"]; ?></div>
  14.                                 <div id="tutorial-<?php echo $tutorial["tutorial_id"]; ?>">
  15.                                 <input type="hidden" id="likes-<?php echo $tutorial["tutorial_id"]; ?>" value="<?php echo $tutorial["likes"]; ?>">
  16.                                 <?php
  17.                                 $query ="SELECT * FROM ipaddress_likes_map WHERE tutorial_id = '" . $tutorial["tutorial_id"] . "' and ip_address = '" . $ip_address . "'";
  18.                                 $count = $db_handle->numRows($query);
  19.                                 $str_like = "like";
  20.                                 if(!empty($count)) {
  21.                                 $str_like = "unlike";
  22.                                 }
  23.                                 ?>
  24.                                 <div class="btn-likes"><input type="button" title="<?php echo ucwords($str_like); ?>" class="<?php echo $str_like; ?>" onClick="moreLikes(<?php echo $tutorial["tutorial_id"]; ?>,'<?php echo $str_like; ?>')" /></div>
  25.                                 <div class="label-likes"><?php if(!empty($tutorial["likes"])) { echo $tutorial["likes"] . " Like(s)"; } ?></div>
  26.                                 </div>
  27.                         <div class="desc"><?php echo $tutorial["description"]; ?></div>
  28.                         </td>
  29.                 </tr>
  30.         <?php          
  31.         }
  32.         }
  33.         ?>
  34.         </tbody>
jQuery Script Like Unlike This script will be run after you click the Like Unlike Icon.
  1. <script>
  2. function moreLikes(id,action) {
  3.         $('.demo-table #tutorial-'+id+' li').each(function(index) {
  4.                 $(this).addClass('selected');
  5.                 $('#tutorial-'+id+' #rating').val((index+1));
  6.                 if(index == $('.demo-table #tutorial-'+id+' li').index(obj)) {
  7.                         return false;  
  8.                 }
  9.         });
  10.         $.ajax({
  11.         url: "more_likes.php",
  12.         data:'id='+id+'&action='+action,
  13.         type: "POST",
  14.         beforeSend: function(){
  15.                 $('#tutorial-'+id+' .btn-likes').html("<img src='LoaderIcon.gif' />");
  16.         },
  17.         success: function(data){
  18.         var likes = parseInt($('#likes-'+id).val());
  19.         switch(action) {
  20.                 case "like":
  21.                 $('#tutorial-'+id+' .btn-likes').html('<input type="button" title="Unlike" class="unlike" onClick="moreLikes('+id+',\'unlike\')" />');
  22.                 likes = likes+1;
  23.                 break;
  24.                 case "unlike":
  25.                 $('#tutorial-'+id+' .btn-likes').html('<input type="button" title="Like" class="like"  onClick="moreLikes('+id+',\'like\')" />')
  26.                 likes = likes-1;
  27.                 break;
  28.         }
  29.         $('#likes-'+id).val(likes);
  30.         if(likes>0) {
  31.                 $('#tutorial-'+id+' .label-likes').html(likes+" Like(s)");
  32.         } else {
  33.                 $('#tutorial-'+id+' .label-likes').html('');
  34.         }
  35.         }
  36.         });
  37. }
  38. </script>
And, this is the result. Result Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment