First really sorry if its a stupid question but I'm a beginner.
So I recently started working with fulephp and I have a small site, and would like to give the users the ability to like other users profile.
But I'm a bit unexperienced at this part. So I have a table users_like
id | users_id | liked_by
1 | 5 | 1
2 | 5 | 3
3 | 1 | 2
4 | 1 | 9
the user_id
is who got the like and the liked_by
is who gave the like
So I have a like button on the users profile
<button class="like btn btn-primary" data-like="<?php echo $user->profile['user_id']; ?>">Like</button>
the data-like
contains the users id who gets the like
And the insert looks like this
PHP function
public function action_like($id)
{
if(Input::is_ajax()):
$response = Response::forge();
$like = Model_Like::forge();
$like->user_id = $id;
$like->liked_by = Session::get('sentry_user');
$like->save();
$response->body(json_encode(array(
'status' => 'liked',
)));
return $response;
endif;// is ajax
}
jQuery ajax posts
$('button.like').on('click', function(){
var likeId = $(this).data('like');
$.ajax({
type: "POST",
url: siteUrl + "profile/like/" + likeId,
dataType: "json",
context: this,
beforeSend: function()
{
$(this).attr('disabled', 'disabled');
},
success: function(data)
{
if(data.status === "liked") {
$(this).removeClass('like')
.addClass('unlike')
.text('Un-Like');
}
},
complete: function()
{
$(this).removeAttr('disabled', 'disabled');
}
});
});
So actually my logic was this.
The orm retuns the users likes, and I was thinking to do this
if(Session::get('sentry_user') != $like['liked_by']) {
<button class="like btn btn-primary" data-like="<?php echo $user->profile['user_id']; ?>">Like</button>
}
else {
<button class="unlike btn btn-primary" data-like="<?php echo $user->profile['user_id']; ?>">Un-Like</button>
}
So if the logged in users id is not equals to the liked_by
than show the button Like otherwise show the button unlike, and if they click that a function will remove the like
But I was silly because I forgot that this actually returns arrays so this is a no-no
So my question is that, I should use jquery cookie somehow to achive the following.
When an user clicks on Like some how get the user has liked that profile and change the button to unlike.
Or could please someone give me a little hint for this solution, just a hint or advice.