Fenhopi Posted June 20, 2010 Share Posted June 20, 2010 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 https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/ Share on other sites More sharing options...
Fenhopi Posted June 20, 2010 Author Share Posted June 20, 2010 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 https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1074528 Share on other sites More sharing options...
gizmola Posted June 20, 2010 Share Posted June 20, 2010 Inner join status and friends2, so you have a single query, ordered by dtime desc. Link to comment https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1074529 Share on other sites More sharing options...
kratsg Posted June 20, 2010 Share Posted June 20, 2010 $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 https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1074531 Share on other sites More sharing options...
gizmola Posted June 20, 2010 Share Posted June 20, 2010 $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 https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1074541 Share on other sites More sharing options...
Fenhopi Posted June 20, 2010 Author Share Posted June 20, 2010 Could you please provide me with an example how I would make a join.. I've looked it up but I don't seem to understand how to put it together right.. Link to comment https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1074697 Share on other sites More sharing options...
kratsg Posted June 20, 2010 Share Posted June 20, 2010 And example of a join query: $friend_statuses = "SELECT status.*, friends2.user1 FROM status, friends2 WHERE byuser=user1 AND user2='$username' ORDER BY status.dtime DESC"; Link to comment https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1074698 Share on other sites More sharing options...
Fenhopi Posted June 21, 2010 Author Share Posted June 21, 2010 Alright, thanks a lot! If I wanted to add myself/$username to the list of statuses, how would I do that? Link to comment https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1074806 Share on other sites More sharing options...
kratsg Posted June 21, 2010 Share Posted June 21, 2010 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 https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1074819 Share on other sites More sharing options...
Fenhopi Posted June 22, 2010 Author Share Posted June 22, 2010 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 https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1075377 Share on other sites More sharing options...
kratsg Posted June 22, 2010 Share Posted June 22, 2010 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 https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1075413 Share on other sites More sharing options...
Ruzzas Posted June 22, 2010 Share Posted June 22, 2010 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 https://forums.phpfreaks.com/topic/205295-selecting-statuses-from-friends-only/#findComment-1075416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.