Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/247800-php-like-this/
Share on other sites

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).

Link to comment
https://forums.phpfreaks.com/topic/247800-php-like-this/#findComment-1272476
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/247800-php-like-this/#findComment-1272639
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/247800-php-like-this/#findComment-1272645
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/247800-php-like-this/#findComment-1272677
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.