Jump to content

"Like" a comment


dean7

Recommended Posts

Hey guys, I've only recently came back into coding as I lost interest although I've got a news script which users can "Like" the news post. Its only a basic script to refresh my memory. Liking the news post all works although how can I check a way that if that user has already liked that news post that user cannot do it agian?

 

I've got this for liking the post:

 

$Like = keepsafe($_GET['like']);if ($Like){  $UpdateLike = mysql_query("INSERT INTO $NewsLikes (`id`,`newsid`,`likedby`,`time`) VALUES ('','".$Like."','".$Username."','".$date."')") or die (mysql_error());echo "Successfully liked that update!"; $GetThingys = mysql_query("SELECT * FROM $TblNews WHERE `id` = '".$Like."' LIMIT 1") or die (mysql_error());$Thingysgot = mysql_fetch_object($GetThingys); $TheNumber = $Thingysgot->likes;$Addon = "1";$NowNumber = $TheNumber + $Addon;$UpdateNewsLike = mysql_query("UPDATE $TblNews SET `likes` = '".$NowNumber."' WHERE `id` = '".$Like."'") or die (mysql_error());  }

 

MySQL Table:

 

id | newsid | likedby | time

 

 

Quite obviously newsid is the ID from the news which they clicked "like" on. Likedby is the username who liked it 

 

Thanks for anyone that helps :)

 

Link to comment
https://forums.phpfreaks.com/topic/287456-like-a-comment/
Share on other sites

Before adding the entry, you could run a query to check if they already liked the page. Just run a search that looks for an entry with the selected news post's ID and the username. If a match isn't found, run your insert query.

 

Additionally, you could replace the "Like" button with something else (maybe an Unlike button) if the visitor already liked the news story they are currently viewing. You would run the same query as described above.

Link to comment
https://forums.phpfreaks.com/topic/287456-like-a-comment/#findComment-1474671
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.