Jump to content

Getting news feed


jbonnett

Recommended Posts

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 by jbonnett
Link to comment
Share on other sites

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 :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.