Jump to content

mysql function help


phpisawesome

Recommended Posts

id  friend_id

7      10

 

10      7

 

7      11

 

11      7

 

So 7 and 10 are friends. 7 and 11 are friends........

 

I have a mysql_fetch_row to see if 2 people are friends...if it equals 0 it shows add me as friend!

 

My problem is it is basically saying 10 and 11 are friends...and doesn't show add me as friend! any help?

Link to comment
Share on other sites

I'm not completely sure what you're trying to do, but I think I have an idea. You want to see if personA is friends with personB?

 

I suggest adding each person's friends to an array that is named for that person. Kinda like this:

 

<?php
while ($row = mysql_fetch_assoc($result)){
  if (!isset(${$row['id']}) ${$row['id']} = array();
  ${$row['id']}[] = $row['friend_id'];
}

 

Then just do an in_array() check to see if they are friends.

Link to comment
Share on other sites

So basically whenever someone adds another friend it adds to the id and friend id...

 

7 10

 

10 7

 

but if there is another group of friends like

 

7 11

 

11 7

 

When 10 views 11s profile it should say add as friend...but when I do the query and fetch the row it shows 10 is 11's friend...help at all?

Link to comment
Share on other sites

Possibly. You should still be able to use the code. Will this do it for you?

 

<?php
$person = 7; // I'm pretending this is your input "person"
$friend = 10; // and this is your input "friend"
while ($row = mysql_fetch_assoc($result)){
  if (!isset(${$row['id']}) ${$row['id']} = array();
  ${$row['id']}[] = $row['friend_id'];
}
if (in_array($person,${$friend})||in_array($friend,${$person})){
  // Do something, because they ARE friends!
}
else{
  // They are NOT firends
}
?>

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.