Show posts from users that I follow, but show my posts too - php

I`m working on a small website where people can follow each other. There are newsfeed on the site where users can see updates from other users that they follow. The problem is that I can see only posts from other users and not my posts. I want the current user to be able to see his posts too Ordered within the other users posts. Something like Facebook Wall.
I have tried tons of differend queries and php, but I get only other users post or when i manage to pull out my posts too there are dublicate post for every user that follows me.
I have 3 tables 'members', 'Post' and 'follow'. The members table hold UserID, Name and Last Name. Post table holds the UserID(FromID in the table) Indexed to members UserID and the post from the user. In the follow table there are 3 columns FollowID, Followed and Follower both Followed and Follower are index to members UserID.
At the moment I use that query:
$query= "SELECT Post.*, members.*, follow.* FROM Post
INNER JOIN members ON Post.FromID=members.UserID
LEFT JOIN follow ON members.UserID=follow.Followed
WHERE Post.FromID='$user' OR follow.FollowerID='$user'
ORDER BY Post.date DESC, Post.time DESC";
$user is the UserID of the current logged in user.
That query returns all the posts that I want but the problem is that my post are show /n times for each user that follows me.
I really would appreciate the kindness to the one that give me some directions what i do wrong. Thank you

The problem is with your $user. Currently what is happening is that you have an OR condition in where clause. And you are trying to pull either your data or ur followers data.
My solution for you would be to remove $user from the query you have. So you will have list of followers data only. Have a different query for your posts and then while displaying sort them by datetime Timestamp so you have your posts with other followers too.
To have your posts and folloeers posts in one query.
Use union. Like :
(Query of your posts ORDER BY date)
UNION
(Query of followers posts ORDER BY date);
Also check UNION usage in w3schools.
Hope this helps

I think this query might be easier to understand:
$query = "SELECT * FROM Post,members,follow
WHERE
Post.FromID=members.UserID
AND
members.UserID=follow.Followed
AND ( Post.FromID='$user' OR follow.FollowerID='$user')
ORDER BY Post.date DESC, Post.time DESC";

Related

PHP SELECT FROM multiple tables

I have a article pages, that i only want to show feed of the users that every account is following, kinda like twitter.
I have to distinct tables one is called posts other is called followers
I want to loop the articles but i want it to select only the users that my account is following.
I just need the SQL query that can do that since the PHP part of the rest i can perform it
Something like this , although i know that this one does not work
$connect->query("SELECT id,userid,postagem FROM posts WHERE IN (SELECT followed FROM followers WHERE whofollowed = '$userd' ) ORDER BY id DESC LIMIT 13");
Assuming followed is also a userid in the followers table:
$connect->query("SELECT id,userid,postagem FROM posts WHERE userid IN (SELECT followed FROM followers WHERE whofollowed = '$userd' ) ORDER BY id DESC LIMIT 13")

Mysql query with multiple tables and foreign keys

Consider this mysql tables structure (useful to store private/group chat messages):
USERS
user_id
username
password
GROUPS (= DISCUSSIONS / TOPICS)
group_id
name
GROUPS_MEMBERS (= MEMBERS OF A SPECIFIC DISCUSSION / TOPIC)
group_id
user_id
MESSAGES
message_id
timestamp
from_user_id
destination_type (enum - group, user)
destination_id
Can you please help me with the query to retrieve the list of the 5 more recent dicussions (either private or group) in which a specific user has been a active?
Important:
I don't have actual code since I'm just deciding how to structure the database tables. The table structure presented above it's pretty self-explanatory (destination_id is a reference to group_id, and group members are all the users that will receive a message. Finally, all messages sent between the users of a specific group make a discussion or topic).
Here is what I want to do (it's very easy... don't over-think it... it's like any chat/messaging system like Facebook or Gmail etc).
When a user logs in and opens the chat he will of course see all the latest discussion which he is/has been a part of. In a chronological DESC order.
So I need to write the query to retrieve the latest 5 GROUP_IDs (= discussions) in chronological DESC order. But only the discussions which the logged-in user is a part of.(Of course I have the id of the logged-in user.. for example 16)
P.s. I didn't build this table structure myself but it seems logic; the only problem is the one presented above.
Here's my suggestion. You can get different records by using DISTINCT and get only five records by using LIMIT. You can replace logged_in_user_id with the login id.
SELECT DISTINCT GROUPS.group_id FROM USERS
JOIN GROUP_MEMBERS ON USERS.user_id = GROUP_MEMBERS.user_id
JOIN GROUPS ON GROUPS.group_id = GROUP_MEMBERS.group_id
JOIN MESSAGES ON destination_type = 'group' AND destination_id = GROUPS.group_id
WHERE USERS.user_id = logged_in_user_id
ORDER BY timestamp DESC
LIMIT 5;
#Igor Carmagna:
I have gone through your question and according to that i think you are required list the top 5 messages which have been left by the end users right ?. So for that please please follow below given steps.
1) First and for most thing you are required to do is that Join.
2) In this step you are required to use max() function which will give you list of the users on the base of messages received. Now, according to your question you are required to have only top 5 records so you are bound use (max-5) function this will given top 5 records
Hope this will make you day !!
Cheers :) :P

Mysql join query for showing friends posts with viewstatus!=public

I have three table tbl_user, tbl_posts, tbl_friends
tbl_user stores all the user datas.
tbl_friends stores which user are friends with other.
tbl_posts, where the users posts are stored. userid is the field where all the users id are stored. p_viewstatus field stores the view status of the posts. if the view status of the my friends posts is only by me then that posts should not shown to other users
If sam have a post with id=40 in tbl_posts. The p_viewstatus of the post is only by me then that post must only be visible to sam only.
If sam have another post with id=41 and p_viewstatus!='only by me' this post must visible to all users who are sam's friend
Tried the following query but don't know how to relate the p_viewstatus
Note: I want to fetch all of my posts and my friends posts from **tbl_posts except the p_viewstatus of my friends posts no equal to 'only by me'**
SELECT * FROM `tbl_posts` as p,`tbl_friends` as f WHERE p.`userid`=f.`userid` and p.`userid`=23
Thanks in advance.
BELOW SCREENSHOT SHOWS THE TABLE CONTENTS AFTER JOIN QUERY.
Here's how your query should look like:
SELECT * FROM `tbl_posts` as p
WHERE p.`userid`=23 OR (p.`userid` IN (SELECT `friendsid` `FROM tblfriends` WHERE `userid`=23)
AND p.`p_viewstatus`<>'only by me')
First you select the data from the table you need. After that you filter it with the user id and the ids of the friends of the user and you also tell it that you only want the posts that are not viewable only by me.

Using an array in an SQL query

Okay, so I want to have a news feed on my website. I have 3 tables named Users, Follow, and Posts. Basic user data goes into the Users table, who is following who is stored in the Follow table, and whatever the user posts goes into Posts. Now, I know how to select every post from a database table and limit it using the WHERE clause. But how would I write my query to select all all of the posts from only user's they are following, and display them by DESC date? Thanks.
Here's a general layout for you, make three tables like you mentioned, I've outlined below just as an example
[TABLES]
users
followers
posts
In the users table you should have at least columns like userid (auto incremented value / primary key).
The followers table should have something like userid which would match to the users table userid, a followid column which would also have the id # for the follower from the users table.
Then for your posts table you would want to have userid too so when each post is made it would have the id from users table.
Then you would need to do something like:
SELECT p.*
FROM posts AS p
WHERE p.userid IN (SELECT followid FROM followers WHERE userid = ###)
ORDER BY p.date DESC
Now it really depends on how you are getting the users id to figure this out. If your passing the users id using a session after they logged in similar to something like Facebook, then you could change userid = ### to something like userid = ".$_SESSION['userid']." But again it really depends on how you pass the users id but the above should at least get you somewhat started.
Make sure to put indexes on the userid, followid columns so that when the table becomes larger it will do the joins quickly.
An alternative to #Shane's answer is to use the JOIN operator:
'SELECT p.* // I prefer to name all the fields, but for brevity's sake I'll use the shorthand
FROM Posts AS p
INNER JOIN Follow AS f ON p.userid = f.followid
WHERE f.userid = '.$selectedUserID.'
ORDER BY p.date DESC;'
For an inputed User ID ($selectedUserID), it will find all User ID's of the people they follow (matching follow ID to user ID on the Follow x-ref table) and then find their respective posts from the Post table in descending order by date. It will return empty if they do not follow anyone or the people they follow have no posts.
I also hope I do not need to remind you to sanitize your input to the database and escape your output to the web.
Is this what you're looking for?

How do I set my friend feed to include user statuses?

I'm making a social networking site and I have two types of statuses. Friends and Public. Users can upload to either. The relevant tables for this question are members, friends, and status. Relevant field for members is Handle. Relevant fields for friends are Friend and Username. Relevant fields for status is Handle and type(with values friends or public).
I managed to call the statuses I needed for only friends, but my question is how do I get it to show friends as well as the user's statuses
Here's the code that shows all statuses from a user's friends.
$shawing = "SELECT *
FROM status
JOIN friends
ON status.Handle = friends.Friend where friends.Username='$members[Handle]' and status.Type='friends' ORDER by status.Id DESC" or print mysql_error();
$members[Handle] calls from an include that identifies the user who is logged in.
I know it's probably simple but how do I correct it to show both the friends AND user statuses?
UNION them together (merge two queries)
$shawing = "
SELECT 1 IsUser, '$members[Handle]' Friend, status.*
FROM status
where status.Handle='$members[Handle]'
and status.Type='friends'
UNION ALL
SELECT 2 IsUser, friends.Friend, status.*
FROM status
JOIN friends ON status.Handle = friends.Friend
where friends.Username='$members[Handle]'
and status.Type='friends'
ORDER by IsUser, status.Id DESC" or print mysql_error();
Change it to a 'right join'. You're blocking on values for 'status' that don't have related entries in 'friends'. You could probably lose lots of the 'where' clause too.

Categories