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:
Echo "<b>Likes:</b> ".$info['likes'] . "</span>";
Echo '<script src="http://code.jquery.com/jquery-latest.js"></script><script type="text/javascript">function test() {$.post("http://stormstorm.com/like.php? id='.$info['fileid'].'")</script>';
Echo '<br /><img onClick="test();" src="img/like.jpg"></p>';
The PHP for the like incremented in like.php:
$id = $_GET['id'];
mysql_query("UPDATE files SET likes=likes+1 WHERE fileid=".$id) or die(mysql_error());
The PHP for the liking works fine, I’m happy with that. But the thing to show the liking just sucks badly I think. Thing is I have a list.php which will print the contents of the database one after the other to print all the image listed. So it will print the same replica of the script over and over, typically hard coding the current image ID into the posting. I’m pretty new to this but feel this code sucks, plus it doesn’t update the images section.
I was thinking to use Javascript to simply get the Likes element and ++ it but, then it hit me. My list will have over 100+ of these same elements. You can probably tell I might be approaching this the wrong way, and I hope someone can help me out with this.
It looks like you have the general idea down, just a crude implementation of it.
You may want to designate the counter element for each image and update the inner content after the button is pressed…
where the {image_id} is obviously unique to each image. Pass that to test({image_id}) and then update the html of the count total on success…
in your php you would do exactly what you did except return a json encoded array back to the js for update…
Keep in mind this is a VERY POOR implementation of this. The real system should definitely be one that does not allow people to just repeatedly click the button over and over again to increment something for the hell of it. According to your needs, of course, you should limit it by login and even record user information relative to the file they’re liking and not just increment a number in the database.
So you would have a relational table that stores information for each like. That way you can query the database to make sure a user has not already liked the file before incrementing the number.
Hope that makes sense.