I’m having trouble getting some data…. I was wondering if someone can help me,
I have 4 tables (likes, follow, comment, users)
I want to be able to populate my page when a user likes/comments/follows/etc… (if a user is following a particular user).
likes
idlikes idusers iditem
1 1 5
2 2 4
3 2 22
follow
idfollow idusers_follower idusers idbusiness
1 1 2
2 1 3
3 1 4
4 4 2
5 4 1
comment
idcomments idusers text
1 1 asfd
2 2 safd
users
idusers
1
2
3
4
For example if I am id user #1, I’m following users #2, #3, #4
My page would populate to show:
- #2 likes item #4, #22.
- #4 is following #2 (because I’m following #4, this is why its showing)
- #2 comments “safd”
I’m not sure what is the best way to display this? I currently have multiple functions querying on table at a time, and I’m working on merging the arrays together? Or should I use join tables? Which I’m trying now…
Get users that I’m following.
$feeds = new feed();
$meID = 1;
$query = "SELECT idusers FROM follow WHERE iduserse_follower = ?";
$users = $dbh -> prepare($query);
$users -> execute(array($meID));
while($following = $users -> fetch(PDO::FETCH_ASSOC)){
$follow = $following['idusers']; //This will get all of useres I'm following
$populate = $feeds->feed_all($follow); // from function
}
Query
class feed()
{
public function feed_all($idusers)
{
// SYNTAX HELP //////////////////////
$query = "SELECT
f.idusers_follower,
f.idusers,
l.iditem,
c.text
FROM follow f, users u
JOIN likes l
ON l.idusers = f.idusers
JOIN comment c
ON c.idusers = f.idusers
WHERE f.idusers_follower = ? AND f.idusers_follower = l.idusers AND f.idusers_follower = c.idusers AND f.idusers = u.idusers"
$pop = $dbh->prepare($query);
$pop ->execute($idusers);
// while loop to return value
while($row = $pop -> fetch(PDO::FETCH_ASSOC))
{
$feed_data[]=$row;
}
return $feed_data;
}
}
This is where I’m stuck. 🙁 I’m not even sure if I’m doing the statement right?
++++++++++ EDIT: ++++++++++++
I have edited to add idbusiness
Now since I’m following #4, it would also show up that #4 is following #1.
Your current approach of performing three separate queries is as good as any; you can combine them into a single resultset using
UNION, which would be useful if you wanted to sort the combined results by some field (e.g. activity timestamp) and/or limit the combined results:See it on sqlfiddle.