I’ve been trying to find an example online of a Facebook style «Like» button but have not been able to find anything like this. What I would like to do is, place a button below an image which the user can press. Once pressed it will increment a value in the database for the image’s record, and then reflect the addition on the page by adding + 1 to the existing amount. I can guess this will need PHP, SQL and jQuery to pull off. Problem is I have no idea where to begin. I’ve created already a PHP script to add to my Like’s for a particular image by giving the image ID. I created already a jQuery post button which posts to the PHP and likes the image. The thing I’m stuck on is updating the page to reflect the like. For starters, I think the code I made to do this so far is completely disgusting lol. Here is all my code so far. PHP to output the Likes count and Like button, plus code for addition. $info is the array for the result of my whole image files table:
$id = $_GET['id']; mysql_query("UPDATE files SET likes=likes+1 WHERE fileid mt24 mb12">
php
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this question" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="question" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="1" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
in your php you would do exactly what you did except return a json encoded array back to the js for update.
$id = mysql_real_escape_string($_GET['id']); mysql_query("UPDATE files SET likes=likes+1 WHERE fileid=".$id); if(mysql_affected_rows() > 0) < $sql = mysql_fetch_assoc(mysql_query("SELECT `likes` FROM `files` WHERE `fileid` mt24">
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
I think you are headed the right direction. You can return the number of likes after the update call in php and let jquery update the like count. I added some code below as an example.
HTML:
PHP: (*Note: I didn't check syntax but hopefully it will be enough code to understand the idea.) $id = $_GET['id']; mysql_query("UPDATE files SET likes=likes+1 WHERE fileid=".$id) or die(mysql_error()); $result = mysql_query("SELECT likes from files where fileid mt24">
Facebook Like Reactions using PHP, MySQL and Jquery.
Just a few days before, Facebook has launched Reactions, an extension of Like button. I succeeded in implementing the same in my WallScript 8, Build Your Own Social Network. Here is a quick and easy way to implement the Reactions using PHP,Jquery and PDO MySQL. It’s a basic idea of implementing the Reactions. Hope you enjoy the new Reactions for Like button. Take a quick look at the live demo.
Database Design To build the message like reaction system, you have to create four tables such as Users, Messages, Message_Like and Reactions. This following image generated by using Mysql Workbench tool.
CREATE TABLE ` users ` ( ` uid ` int NOT NULL PRIMARY KEY AUTO_INCREMENT , ` username ` varchar( 25 ) NOT NULL UNIQUE, ` password ` varchar( 50 ) NOT NULL , ` email ` varchar( 100 ) NOT NULL, ` name ` varchar( 100 ) NOT NULL, ` profile_pic ` varchar( 200 ) NOT NULL, );
Data will store in following way, here the password data PHP login script with PDO and HASH encryption.
This table contains user status messages data. Here uid_fk is the FOREIGN KEY to REFERENCES users.uid
CREATE TABLE reactions ( ' rid ' INT NOT NULL PRIMARY KEY AUTO_INCREMENT , ' name ' VARCHAR( 45 ) NOT NULL );
Contains all user message likes relation data. Here uid_fk is FOREIGN KEY to REFERENCES users.uid, rid_fk is FOREIGN KEY to REFERENCES reactions.rid and msg_id_fk is FOREIGN KEY to REFERENCES messages.msg_id
Like Ajax Contains javascript code. $("body").on('click','.likeTypeAction', function()<>- here likeTypeAction is the class name of the reaction icon tag. Using $(this).parent().parent().attr("id") calling icon tag message ID value.
/*Reaction*/ $(" body "). on (" click "," .likeTypeAction ",function() var reactionType=$(this). attr ("data-reaction"); var reactionName=$(this). attr ("original-title"); var rel=$(this). parent (). parent (). attr ("rel"); var x=$(this). parent (). parent (). attr ("id"); var sid=x. split ("reaction"); var msg_id=sid[1];
Unlike Ajax Contains javascript code. $("body").on('click','.unLike', function()<>- here unLike is the class name of the unlike anchor tag. Using $(this).attr("id") calling anchor tag message ID value.
index.php Contains PHP and HTML code, displaying all of the user updates by calling $feed->newsFeed();