jbonnett Posted May 5, 2013 Share Posted May 5, 2013 (edited) I'm not sure if I posted this in the right tread but here goes, I'm trying to get a list of friends from a database and from them results get all posts to do with them friends and then user themselves. Here's what I have come up with <? $friends = $database->query("SELECT * FROM `".TBL_FRIENDS."` WHERE `person_id` = '$session->username' AND `status` = 'Active' OR `friend_id` = '$session->username' AND `status` = 'Active'"); while($f = mysql_fetch_array($friends)){ $pid = array(); if($f['friend_id'] == "$session->username"){ $posts = $database->query("SELECT `id` FROM `".TBL_STATUS."` WHERE `person_id` = '".$f['person_id']."' OR `friend_id` = '".$f['person_id']."' ORDER BY `timestamp` DESC"); while($p = mysql_fetch_array($posts)){ if(!in_array($p['id'], $pid)){ $pid[] = $p['id']; } } }else{ $posts = $database->query("SELECT `id` FROM `".TBL_STATUS."` WHERE `person_id` = '".$f['friend_id']."' OR `friend_id` = '".$f['friend_id']."' ORDER BY `timestamp` DESC"); while($p = mysql_fetch_array($posts)){ if(!in_array($p['id'], $pid)){ $pid[] = $p['id']; } } } $posts = $database->query("SELECT `id` FROM `".TBL_STATUS."` WHERE `person_id` = '$session->username' OR `friend_id` = '$session->username' ORDER BY `timestamp` DESC"); while($p = mysql_fetch_array($posts)){ if(!in_array($p['id'], $pid)){ $pid[] = $p['id']; } } } $count = count($pid); for($i = 0; $i < $count; $i++){ if($av != "true") { $visible = " AND `visibility` = 'true' "; } $posts = $database->query("SELECT * FROM `".TBL_STATUS."` WHERE `id` = '".$pid[$i]."'$visible ORDER BY `timestamp` DESC"); $p = mysql_fetch_array($posts); if($p['post_type'] == "status"){ include("include/status.php"); }elseif($p['post_type'] == "photo") { include("include/photos.php"); } } ?> I have to do separate query's per person as I have to determine whether the friend is in the "person_id" or the "friend_id" column first of the otherwise my code won't work. The queries are not showing all posts Any help is much appreciated. Edited May 5, 2013 by jbonnett Quote Link to comment Share on other sites More sharing options...
Barand Posted May 5, 2013 Share Posted May 5, 2013 This recent thread may help http://forums.phpfreaks.com/index.php?showtopic=277504&hl=%2Bmessage+%2Bfacebook Quote Link to comment Share on other sites More sharing options...
jbonnett Posted May 5, 2013 Author Share Posted May 5, 2013 I can't do this as I need to determine witch combination the two columns are going to with the two tables e.g. if user 1 adds user 2, database +----+---------------+--------------+-----------+ -- id + person_id + friend_id + status -- +----+----------------------------=+-----------+ + 1 + user1 + user2 + active -- +-----+-------------+--------------+-----------+ user 2 posts a status +----+---------------+--------------+-----------------+-------------------+ - id + person_id + friend_id + status_type - text - +----+----------------------------=+-----------+------+------------------+ - 1 + user1 + user2 + status - Hello world - +-----+-------------+--------------+-----------+------+------------------+ now the problem is if the status "person_id" and "friend_id" are the other way around then my query is invalid so i need to make more than one, athough if I make more than one the "ORDER BY" won't sort properly also if I use more than one query then the results are random to who is in the news feed or I get duplicate results * friends. I can't win Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.