Jump to content

**SOLVED** Using an array in a query, possible?


cstegner

Recommended Posts


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 query

so say the array is $friends

SELECT * FROM bulletins WHERE poster=$friends LIMIT 7

I 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.
Link to comment
Share on other sites

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

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 ;)
Link to comment
Share on other sites

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

  • 2 weeks later...
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
Link to comment
Share on other sites

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