I am trying get the last three posts and statuses, as I know the status table contains only the status that I put on my Facebook page. The posts table contains a shared link, posts made by me or other users, so I want to: compare the last three statuses with the last three posts and take only the most 3 new (created date) from them, so I need to make check the created date or updated date for both sides and the output should be three.
I have tried:
Take the last three posts and the last three statuses, compare the updated date with the most updated three, put them in an array. That took a lot of PHP code. Can I do that with FQL?
You can get the last three posts and statuses via Graph API. Use:
https://graph.facebook.com/fb_id?fields=id,name,statuses.limit(3).fields(message),posts.fields(message,caption,description)
For more help, https://developers.facebook.com/tools/explorer
FQL
For getting statuses
SELECT status_id, message FROM status WHERE uid=fb_id
For getting posts
SELECT post_id, actor_id, target_id, message FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0
But for all these you should have the permissions for accessing them. See
http://developers.facebook.com/docs/reference/fql/stream/.
Related
I have a screen that looks very much like facebook timeline
users can view posts of other users etc.
to get these posts i do something like
select user.id,user.name,posts.title,posts.body from posts left join users;
now data i need to collect is "Who saw this post" .
is there any elegant way to do it ?
right now all what i can think of is every time i fetch posts. i loop over them, then collect all ids of posts that the query returned and then push in another table
user_views [pk:user_id+postId]
userId,postId
1 , 1
Then when i'm fetching posts next time i can do count of user_views.
select *,count(user_views.id) from posts join user_views on post_id = post.id
but this sound like a lot of work for each VIEW, specially that most probably user will see a most multiple times,
is there any known patterns for such need ?
This is a design question and the answer really depends on your needs.
If you want to know exactly who viewed what post and how many times, then you need to collect the data on user - post level.
However, you may decide that you do not really care who viewed which post how many times, you just want to know how many times a post was viewed. In this case you may only have a table with post id and view count fields and you just increment the view count every time a post is being viewed.
Obviously, you can apply a mixed approach and have a detailed user - post table (perhaps even with timestamp) and have an aggregate table with post id and view count fields. The detailed table can be used to analyse your user's behaviour in a greater detail, or present them a track of their own activities, while your aggretage table can be used to quickly fetch overall view counts for a post. The aggregate table can be updated by a trigger.
I am creating a social website.I have an alert option which needs to show the friend requests and unread messages together based on the time it arrives in various div's .Actually I am lack of logic about how to display results from two tables.Kindly help me.
My tables are:
1)sk_friends=>[friendship_id, from_user_id, to_user_id, status,date]
status contains accepted or pending as values.
2)sk_messages=>[msg_id, from_user_id, to_user_id,text, date, status]
SELECT * FROM sk_friends JOIN sk_message ON sk_friends.from_user_id = sk_message.from_user_id
this will be the basic query for retrieving records from two tables..
try this.. but not sure how much will help you
If you're trying to pull both pending friend requests and messages at the same time, you'll probably want to consider adding a more generic "notifications" style table, or run them as separate queries.
So you could go for a table along the lines of:
sk_notifications => [id, date, type, from_user, to_user, status]
Which would contain both types of events (new messages and new friend requests) - your code would then be responsible for providing the different actions based on the notification type.
for friend request see the query below
select * from sk_friends where (logged_in_user_id in from_user_id or logged_in_user_id in to_user_id) and status='pending';
and for message unread
select * from sk_messages where to_user_id=logged_in_user_id;
If I have 10 friends how do i get the latest status for each of them and then post a comment on all of these statuses?
how do you get the post ID?
$facebook->api('/'.$POST_ID.'/comments','post',array('message' => $comment));
I can't find a way to get friends statuses ordered by date from the Graph API, so you'll have to use FQL. This query will get you the 10 most recent status updates by your friends:
SELECT uid,status_id,time,message FROM status WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY time DESC limit 10
There is no guarantee that these 10 will come from different friends since there is no way to execute a UNIQUE query in FQL. If you want to ensure you're commenting on 10 unique friends' statuses, you'll have to request more results and keep track of whose posts you're commenting on in your script.
From here, you can loop through these results and set $POST_ID = $result['status_id'] then execute your API call above.
If this is for a public app you're building, you'll want to look at combining these 10 updates together into a single batch.request to keep from running into the API call limits.
I'm able to read the stream from a group but I'm particularly looking for the posts where it's mentioned that so-and-so has been "added by" a member and on which date. Eg:
John Smith and 12 other members were added by Alexander John.
Unlike · · Unfollow Post · Saturday at 4:02pm
Unfortunately, I can't seem to extract the above info using 'method'=>'fql.query' on the stream table. Even if I manage to find the Ids of these "added by" posts, when I run it as a query, i get an empty array. For example:
$sql = "SELECT post_id, permalink, created_time, updated_time, actor_id, message, attachment, likes, comments FROM stream WHERE gid=$gid AND (post_id='243493399035757' OR post_id='243503932368037')";
If this info shows up on a Group's stream, then it has to be coming from somewhere, right? So, does anyone know how I can get/extract these "added by" posts using FB's API or otherwise??
I'm currently making my own social network and I have a php problem which I do not know how to solve.
So my aim is to display the posts made by the user himself and his friends whereas if you weren't their friends, their posts would be hidden to you.
I have done many research trying to find the answer aswell as implementing the answers but nothing seems to work for my script.
So, here's what my table looks like:
users: key, id, fullname, username etc...
posts: id, feedusername, author, feedpost
shares(AKA friends): id, userId1, userId2, status
At the moment, I am displaying the posts with a while loop.
while($feedarray = mysql_fetch_array($search)) {
*I pull the users post data here by using that fetch_array function and echo them out*
}
And the $search SQL code is this:
$search = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 0,20");
At the moment, this displays all of the posts regardless if you're friends or not friends with that user. Which is not what I want.
So, how do I display my own posts and my friends posts? And if your not friends with lets say, "a", then how do you not display the post's made by "a" to that user who isn't friends with "a"?
In other words, if chaotic(userId1) is friends with max(userId2) and their status is equal to 2(if status is equal to 2, then they are friends. If it's 1, the friend request is pending.)
How do I display my own posts(chaotic's posts) and Max's posts? Whereas If this record does not exist or status is equal to 1, I wouldn't be able to see Max's posts.
Hope you understand what i'm saying and thank you for your time.
Thanks!
ChaoticS
So from what you're saying I gather getting your own posts would involve something like:
SELECT *
FROM posts
WHERE author = #MyUserId
So, given we now want friends, think of how you retrieve friends. It's going to be something like:
SELECT userid2
FROM shares
WHERE userid1 = #MyUserId
AND status = 2
(And may have to make another query reversing the userid1/userd2 columns, depending how your table is setup). So, given that, it's a matter of combining the two forms:
SELECT *
FROM posts
WHERE author = #MyUserID
OR author IN (
SELECT userid2
FROM shares
WHERE userid1 = #MyUserId
AND status = 2
)
You can keep expanding on how you use sub-selects, etc. to be more inclusive/exclusive, but I'll leave that up to you. If you're still having trouble leave me a comment and I'll do my best to help.