0

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.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Side
  • 1,753
  • 9
  • 35
  • 64
  • Can't understand ur question fully. The problem is that button like and unlike does not working as expected? – dark_gf Nov 08 '12 at 16:35
  • the problem is that im not able to think out a logic, what would create the following: if user likes change the button to unlike and keep i, sorry if my question wasnt clear – Side Nov 08 '12 at 16:52
  • khhmm, i'm don't vote u down, and i can't vote down anyway(need 125 rating) – dark_gf Nov 09 '12 at 13:22

1 Answers1

-1

In my point of view i do this task such:

1)user login and we save it id to session data

2)when user request some page we return button "like" or "unlike" (recommend to output 2 buttons one hidden, see below)

3)when user clicks "like": do post by jquery like and after success u mush change to button to unlike it can be done by changing style, text, events, etc, but i would u recommend to hide "like" button and show "unlike"

4)when user clicks "unlike": same as 3) but change to "like" button on success

PS: at server side i recommend to return to true or false, on success or fail.

dark_gf
  • 684
  • 5
  • 15
  • thats nice thank you but, how can i keep the status? i mean when an user likes keep the unlike state? for example after page refresh – Side Nov 08 '12 at 17:28
  • u save data about like's in table users_like, get it from there and do output when user refresh page. – dark_gf Nov 08 '12 at 17:47