My website has a users search where a user can search for other users. I would like for that search to show all friends first, and when there are no friends found, other users. But to do that, I have to run a few db queries on every keyup. The query for friends is somewhat complicated, because there types of friends and believe it is not good for performance to use it all the time, but to get all people by search is a very basic query.
Now I would like to keep those friends in a variable, but I’m getting lost. What would be the best way to do that. I’m already storing info about user with friends in a $_SESSION, but it would be awesome to have that client side. I’m considering storing that in a cookie, but am wondering if that will fit in those 4kb if I try to store an array with like couple hundreds users. What would be the best way to implement that? Any thoughts?
Do you use a cross reference table in the DB to keep track of who is friends with who? I think that’s a good way to solve it if you don’t and then you’d only have to query this table to get the friends.
...because there types of friends...Does this mean that there are different types of friends?
Simplified you could have a table with three columns: person_id, friend_id, friend_type (if that was needed). Person_id and friend_id are references to the same type of id. When two people become friends you add their id’s to the table and what type of friends they are.
If you really want to do this on the client side I guess localStorage could be something to look in to, but it might not be supported by all browsers. localStorage can take around 10MB (if I’m not wrong) of storage. Load the list of friends when a user enters the site the first time. I’d probably go for a session on the server side but that’s just my opinion.