Jump to content

Selecting statuses from friends only?


Fenhopi

Recommended Posts

Hi, I have a database that holds friendships. I've created a database called statuses. Everytime a user makes a status it's added in to the status database. Now I need to make a system that retrieves statuses of friends only.. How would I do that? Thank you!

Link to comment
Share on other sites

So this is what I have:

  if($session->logged_in){
		echo "<img src=\"images/oodlestreamsign.jpg\">";
		$username = $_SESSION['username']; 
		$friends = "SELECT user1 FROM friends2 WHERE user2='$username'";

		$result = $database->query($friends);

		// Loop
		While($myrow = mysql_fetch_array($result))
		{
			//VARS
			$user = $myrow['user1'];
			$status = "SELECT * FROM status WHERE byuser='$user' ORDER BY dtime DESC";
			$statusresult = $database->query($status);
			$myrow2 = mysql_fetch_array($statusresult);

			//Echoes
			echo "By <a href=\"userinfo.php?user=$user\">$user</a>";
			echo "<h3>";
			echo $myrow2['status'];
			echo "</tr></td>";
			echo "<br><tr><td>On: <i>";
			echo $myrow2['dtime'];
			echo "</b></td></tr><tr><td><br>";
			echo "</i></td></tr><hr><br>";
		}

	}
	else{
		echo "Not a lot of Statuses";
	}

 

This works, the only problem is that the statuses by users are displayed in the order I have added my friends, instead of in the order my friends posted their statuses..

 

Help please?

Link to comment
Share on other sites

$status = "SELECT * FROM status WHERE byuser='$user' ORDER BY dtime DESC";

$friends = "SELECT user1 FROM friends2 WHERE user2='$username'";

 

Here's a better query that might suit you: (I haven't tested it yet, but it should work)

 

$friend_statuses = "SELECT * FROM status WHERE byuser=(SELECT user1 FROM friends2 WHERE user2='$username') ORDER BY dtime DESC";

Link to comment
Share on other sites

$status = "SELECT * FROM status WHERE byuser='$user' ORDER BY dtime DESC";

$friends = "SELECT user1 FROM friends2 WHERE user2='$username'";

 

Here's a better query that might suit you: (I haven't tested it yet, but it should work)

 

$friend_statuses = "SELECT * FROM status WHERE byuser=(SELECT user1 FROM friends2 WHERE user2='$username') ORDER BY dtime DESC";

 

A join is preferable to a subquery, but your query won't work regardless, because DTIME is not a column in the friend2 table, which is why a join is needed.

Link to comment
Share on other sites

There are two ways:

 

1.) Just retrieve your own status regardless and just add that to the list.

2.) Re-write the query to be something like...

 

$friend_statuses = "SELECT status.*, friends2.user1 FROM status, friends2 WHERE status.byuser IN (friends2.user1,'myself') AND friends2.user2='$username' ORDER BY status.dtime DESC";

Link to comment
Share on other sites

I couldn't get the query working, it kept echoing my status twice.. here is what I got:

	echo "<img src=\"images/oodlestreamsign.jpg\">";
		$username = $_SESSION['username'];
		$friend_statuses = "SELECT status.*, friends2.user1 FROM statuses, friends2 WHERE statuses.byuser=friends2.user1 or '$username' AND friends2.user2='$username' ORDER BY statuses.dtime DESC";


		$result = $database->query($friend_statuses);


		// Loop
		While($myrow = mysql_fetch_array($result))
		{
			//VARS
			$user = $myrow['byuser'];


			//Echoes
			echo "By <a href=\"userinfo.php?user=$user\">$user</a>";
			echo "<h3>";
			echo $myrow['status];
			echo "</tr></td>";
			echo "<br><tr><td>On: <i>";
			echo $myrow['dtime'];
			echo "</b></td></tr><tr><td><br>";
			echo "</i></td></tr><hr><br>";
		}
		echo mysql_error();
	}

Link to comment
Share on other sites

Can you do the following? After the appropriate variable, write

 

var_dump($friend_statuses)

 

Copy and paste the query that is outputted on the page, go to phpMyAdmin (or whatever you have), run the query, and view the outputted results.

Link to comment
Share on other sites

Can you do the following? After the appropriate variable, write

 

var_dump($friend_statuses)

 

Copy and paste the query that is outputted on the page, go to phpMyAdmin (or whatever you have), run the query, and view the outputted results.

 

you forgot ; after var_dump($friend_statuses)

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.