cstegner Posted April 9, 2006 Share Posted April 9, 2006 I have a page simular to myspace and on peoples homepages I want to display the last seven bulletins there friends posted. But only the bulletins from their friends.No idea how to do this and keep two needed things intact.1) exactly seven bulletins listed.2) only bulletins from friends.Sould I make an array of all of the persons friends then use the array in queryso say the array is $friendsSELECT * FROM bulletins WHERE poster=$friends LIMIT 7I have not started any development on this so there is no need to make do with large mistakes made on my behalf.Thanks for the help as always. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 9, 2006 Share Posted April 9, 2006 If you have an array of friends' usernames$friends = array ('user1', 'user2', 'user3')then you can select posts from just those friends by putting them in a comma-separated list so you have a WHERE clause like "... WHERE poster IN ('user1', 'user2', 'user3')"[code]$friend_list = join ("','", $friends);$sql = "SELECT * FROM bulletins WHERE poster IN ('$friend_list') LIMIT 7";[/code] Quote Link to comment Share on other sites More sharing options...
cstegner Posted April 9, 2006 Author Share Posted April 9, 2006 Awesome exactly what I was hoping, I will go and try it out and replt back. Thanks Quote Link to comment Share on other sites More sharing options...
cstegner Posted April 9, 2006 Author Share Posted April 9, 2006 Can't get to work, this is where I am with it.[code]$a_friend_query = "SELECT * FROM friends WHERE asked='$username' and yes='1' or asking='$username' and yes='1' ORDER BY id DESC";$a_friend_sql = mysql_query($a_friend_query);$friends_array = mysql_fetch_array($a_friend_sql);$friend_list = join ("','", $friends_array);$clean_bulletin_query = "SELECT * FROM bulletin WHERE poster IN ('$friend_list') LIMIT 7";$clean_bulletin_result = mysql_query("$clean_bulletin_query");[/code]And it is not returning anything. Please let me know, this is all I now have left haha ;) Quote Link to comment Share on other sites More sharing options...
cstegner Posted April 9, 2006 Author Share Posted April 9, 2006 Help please Quote Link to comment Share on other sites More sharing options...
Barand Posted April 9, 2006 Share Posted April 9, 2006 Because you use "SELECT * " there is no way of knowing the columns in your table so I am going to assume there is a column "friendname" containing the required names.Try[code]$friends_array = array();$a_friend_query = "SELECT * FROM friends WHERE ((asked='$username') OR(asking='$username')) AND yes='1' ORDER BY id DESC";$a_friend_sql = mysql_query($a_friend_query);while ($row = mysql_fetch_array($a_friend_sql)) { $friends_array[] = $row['friendname'];}$friend_list = join ("','", $friends_array);$clean_bulletin_query = "SELECT * FROM bulletin WHERE poster IN ('$friend_list') LIMIT 7";$clean_bulletin_result = mysql_query("$clean_bulletin_query");[/code] Quote Link to comment Share on other sites More sharing options...
cstegner Posted April 9, 2006 Author Share Posted April 9, 2006 Hey thanks for sticking with me on this, got it figured out thanks a lot for all your help!!! Quote Link to comment Share on other sites More sharing options...
cstegner Posted April 19, 2006 Author Share Posted April 19, 2006 I thought the problem was solved...Here is the deal. The technique does work, except for the fact that I have--since it is my site--around 8,000 friends. So I don't think it is able to process the array for me in time for query. It works for everyone else, even people with a few hundred friends but not for me.So new question. Is there a way to pause the rendering of the page till the array is finished? Or some way to get past this. Not a huge problem this second but a few months down the road when people get more friends could be a disaster.Thanks again, - chris Quote Link to comment Share on other sites More sharing options...
Barand Posted April 19, 2006 Share Posted April 19, 2006 As you only show 7 in second query, limit the first query to a lower number of friends.Or do it all ina single query...[code]$clean_bulletin_query = "SELECT b.* FROM friends f INNER JOIN bulletin b ON f.friendname = b.poster WHERE ((f.asked='$username') OR(f.asking='$username')) AND f.yes='1' ORDER BY b.id DESC LIMIT 7";$clean_bulletin_result = mysql_query($clean_bulletin_query);[/code] 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.