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
https://forums.phpfreaks.com/topic/126123-mysql-function-help/
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
https://forums.phpfreaks.com/topic/126123-mysql-function-help/#findComment-652162
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
https://forums.phpfreaks.com/topic/126123-mysql-function-help/#findComment-652165
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
https://forums.phpfreaks.com/topic/126123-mysql-function-help/#findComment-652170
Share on other sites

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.