Jump to content

Selecting statuses from friends only?


Fenhopi

Recommended Posts

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?

$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";

$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.

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";

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();
	}

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.