dean7 Posted September 24, 2011 Share Posted September 24, 2011 Hey all, I've coded a news system in which on that I want to have something where Users can "Like" or "Dislike" the News Post - Abit like Facebook with the Liking. But theres a few questions which I'm not sure of : - How would I store the Users that have pressed the "like" or "dislike" button in the Database? - also what would be the best idea on selecting the user from the Database to show who has Like or Disliked it. Thanks for any help given. Quote Link to comment https://forums.phpfreaks.com/topic/247800-php-like-this/ Share on other sites More sharing options...
Nethox Posted September 24, 2011 Share Posted September 24, 2011 you need a table with : id (auto_increment) uid (user id) pid (post id, video id, whatever the item is) ip (ip address of the user, so no one can make multiple accounts to like/dislike) When a user clicks "like" you need to record his id + the id of the item he liked + his IP and put that in the DB to fetch all the things that a user has liked, you do an SQL query with WHERE uid = $user_id and with a loop echo everything he liked. to fetch all the "likes" or "dislikes" on an item you do WHERE pid = $item_id with a "count" function to have the number. and you make an IF statement that detects if ($current_user_id == $db_id || $current_user_ip == $db_ip) to make sure he didnt already vote that's the basic approach of doing it. EDIT: I guess you also need to remove the "like" if that same user decides to change his vote to "dislike", keep that in mind too. You'll have to do an SQL query removing his like from the "likes" table and an SQL query adding the dislike to the "dislikes" table. You'll have to check that in the processing part of your PHP (if he's already voted like or dislike). Quote Link to comment https://forums.phpfreaks.com/topic/247800-php-like-this/#findComment-1272476 Share on other sites More sharing options...
jcbones Posted September 25, 2011 Share Posted September 25, 2011 You would make sure the user has voted by checking the user id, IP's can be spoofed, and/or proxied. Which would allow or deny a lot more than just the one user. Quote Link to comment https://forums.phpfreaks.com/topic/247800-php-like-this/#findComment-1272504 Share on other sites More sharing options...
dean7 Posted September 25, 2011 Author Share Posted September 25, 2011 Thanks for your replys, and that has helped me code it , although I seem to have one problem which I can't find a way of getting around.. I have a new Table in my Database which holds what Uses have pressed the Like button , like stated above, although I'm trying todo it so if they have already pressed like it will show them the dislike button rather than the like button. I've got: if ($Username == $Likeobj->username){ $LikeMe = "<td class='UnderTable' align='center' width='10%'><a href='?unlike=".$update[id]."' title='Unlike This News Post?'>Unlike</a></td>"; }elseif ($Username != $LikeObj->username){ $LikeMe = "<td class='UnderTable' align='center' width='10%'><a href='?like=".$update[id]."' title='Like This News Post?'>Like</a></td>"; } Although that's only showing them the Unlike button once they have already pressed the Like button. Any other ways on doing this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/247800-php-like-this/#findComment-1272639 Share on other sites More sharing options...
Nethox Posted September 25, 2011 Share Posted September 25, 2011 I'm not sure exactly of what you're trying to do but why don't you put both buttons in the elseif? Wouldn't it show both buttons then? Quote Link to comment https://forums.phpfreaks.com/topic/247800-php-like-this/#findComment-1272642 Share on other sites More sharing options...
dean7 Posted September 25, 2011 Author Share Posted September 25, 2011 I'm not sure exactly of what you're trying to do but why don't you put both buttons in the elseif? Wouldn't it show both buttons then? Not completely sure what your saying, but basicly I've got something like this at the mintue: Subject Content DateLike But when the User has liked the News post, I'd like the "Like" to changed to "Dislike" so they can dislike the news post if wished later on, but I can only get it to show Like untill they click it then it says Dislike. Quote Link to comment https://forums.phpfreaks.com/topic/247800-php-like-this/#findComment-1272645 Share on other sites More sharing options...
cyberRobot Posted September 25, 2011 Share Posted September 25, 2011 It seems like you have a solution that works, so I'm not exactly sure what you're asking. If you're looking for more efficient code, you could just use "else". if ($Username == $Likeobj->username){ $LikeMe = "<td class='UnderTable' align='center' width='10%'><a href='?unlike=".$update[id]."' title='Unlike This News Post?'>Unlike</a></td>"; }else{ $LikeMe = "<td class='UnderTable' align='center' width='10%'><a href='?like=".$update[id]."' title='Like This News Post?'>Like</a></td>"; } Or you could go even further with the Ternary Operator: $LikeMe = "<td class='UnderTable' align='center' width='10%'>"; $LikeMe .= ($Username == $Likeobj->username) ? "<a href='?unlike=".$update[id]."' title='Unlike This News Post?'>Unlike</a>" : "<a href='?like=".$update[id]."' title='Like This News Post?'>Like</a>"; $LikeMe .= "</td>"; Note that I separated out the table column code since it's the same for both cases. Quote Link to comment https://forums.phpfreaks.com/topic/247800-php-like-this/#findComment-1272677 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.