So I have 3 tables: users, posts, private. In this example, lizzy created a private post 'Dating' and only wants her girlfriends to see it 'authorized_user_ids' in the private table, and herself 'user_id' in posts table.
users
user_id user_name
1 tony
2 steph
3 lizzy
4 adam
5 lara
6 alexa
posts
post_id user_id post_name private (0 is public, 1 is private to authorized users)
1 1 Coding 0
2 3 Dating 1
3 3 Show Me 0
private
private_id post_id authorized_user_ids
1 2 2,5,6
Only the private poster user_id and the authorized_user_ids should see the 'Dating' post by lizzy, but everyone should be able to see the 'Show Me' post by lizzy. Logged out users and users not authorized should not see anything. Is that the most efficient way to go about it, or should it be:
private
private_id post_id authorized_user_id
1 2 2
2 2 5
3 2 6
That's my first question. My second is what would be the best way to tackle it in php (display to authorized_user_ids) when pulling the data from a mysql db? Do we do a second query based on the 'private' flag in the posts table? If it is 1, we query in the while loop to get the 'authorized_user_ids' from the private table, and if it matches the $logged_in_id of the logged in user, then we display 'Dating'? Or should there be a join somewhere in the main query with the 'authorized _user_ids' using the 'post_id' in the private table and the posts table? Any help on how to most efficiently do this is appreciated.