This topic is locked

creating a Favorite button that changes if record has been favorited

5/27/2022 3:50:01 PM
PHPRunner General questions
W
wfcentral author

I have a table called users.
I have another table called favorite_users

I successfully created a button called "Favorite" using the SweetAlert features. When my users click on this button it adds a record in favorite_users that has the logged in userID and the userID of the person they just favorited (stored in a field called favID).

This is for a gamer database where people are able to favorite gamers they want to contact.

I'm stumped on the next part and not sure where to look.

I would like it so if they have favorited a person that button should change to a different color and let them UN-Favorite them.

Maybe I need to write a script that is in each row and it searches the favorite_users table - it it doesn't find a match it shows the "Favorite button" and if it does find a match it shows the "UnFavorite button".

I want the LOOK OF THE BUTTON to change so when you look at the list of names you can see who you have favorited.

Sergey Kornilov admin 5/27/2022

I think the easiest option is to create two buttons and hide one of them based on the condition.

In order to determine if the gamer was already favorited or not you can either perform an additional SQL query for each record or you can modify the original SQL query joining favorite_users via LEFT OUTER JOIN. The joined column will either have an ID of the user/gamer or null.

W
wfcentral author 5/27/2022

modify the original SQL query joining favorite_users via LEFT OUTER JOIN. The joined column will either have an ID of the user/gamer or null.

I tried this solution as it seemed easier. Now if other people have also favorited this gamer they show up twice or more on my list. So, it seems I need something like a JOIN where it is also looking for logged in user id in the favorite_users table - but, I don't know how to put PHPRUNNER values into the mysql statement.