So I'm a bit lost at this part. On my site I would like the give the ability for users to like profile pages.
So my logic is this.
I have a database table named user_likes
, this contains 3 rows, id, user_id, liked_by
.
The actual profile page has a like button on it with a class like.
When the user clicks on it in inserts the data in to the user likes table
and changes the button text to unlike and the class to unlike too with ajax.
And this is the part where I'm stuck.
So the profile has a has many relation likes
, I can count the likes received, but I'm stuck at that part how to keep the button unliked after page refresh.
Because my logic was this (but its a fail): onclick the button, data insert with ajax, button change with ajax, grab the liked by id and if that equals to the logged in users id keep the button unliked.
But since it returns a list of array I can't do that.
So I don't want anybody to code it for me, I would just like a hint for this if its not a big request.
I saw that you posted the same question on the FuelPHP forum, so here I will try to give you another solution.
Why your relation needs "id, user_id, liked_by"? I think you can improve you database modifying a little your tabel.
Reading your question I think you have a structure like that:
// USER TABLE
id
username
... something more
If you create a table named "LIKE" you'll have this situation:
// LIKE
id
created_at
modified_at
... something more
So now you can create a many-to-many relation with a table named "users_likes" with:
// USERS_LIKES
profile_id // it wil be related to the user_id, is the user's profile being liked
liker_id // it will be the user_id of the user that likes the profile
So if you want to retrieve the number of the like you'll have all the information in the current user model or in the profile user model.