I am making a website where you can like blog posts as well as user updates, so should I use insert statement for each likes of user passing post/blog id’s or should I use an array and save likes of user in a single comma separated array
Like this:
Post 1 Example SQL:
-------------------------------------------
Likes(user_id) postid
-------------------------------------------
user1 post1
user2 post1
Or Array
-------------------------------------------
postid likes
-------------------------------------------
post1 user1, user2, user3
Moreover how facebook shows posts on wall randomly, I mean if I store updates in a table and when I loop using those updates, I get updates in line, now How I can add photos uploaded in between or a person adding another as a friend etc etc, I mean are they showing news feeds and linking with the original posts
Do you ever care about getting the individual likes back, unliking something, searching for likes by user, or perhaps even searching for likes by friends of a user? If so, save one record per user-post, and don’t use a comma-separated list/array. The list/array will get unmanageable very quickly and honestly, they are just not what relational databases are good at doing.
If you need to display a comma-separated list on the screen, you can use
GROUP_CONCATto get a string for display.As far as the second question goes, I’m sure there’s some sort of “complicated algorithm” going on, based on what the user often clicks on, to show the user more of “what they like” from their friends. After that it’s probably just a random smattering of all kinds of “wall-item” in newer-things-first order, where “wall-item” can be of type (update, news, video, image, check-in, app-access). I additionally suspect there’s something else going on with the ordering, because the order is most definitely not posting time, nor last time touched.
I also remember way back when some user settings where you could choose the weightings of different kinds of posts (e.g. 10x updates, 3x news, 5x video, 5x image, 1x check-in, 1x app-access) which would change the “randomness” of what comes out the other end. In this case, naively, it would be 1d25, mapped to 1-10=>updates, 11-13=>news, 14-19=>video, etc.