简体   繁体   中英

Get friend's IDs of my friends in PHP?

I am having a table in my database called users_friendships which basically looks like the following:

id     uid1    uid2
-----  -----   -----
1      1       4
2      1       2
3      2       10
4      3       6
..     ..      ..

Through a query, I am already getting the friend's IDs of mine. $myid symbolizes the ID of me:

    // GET FRIENDS ID
    $friendsID = array();

    $my_friends = $mysql->prepare("
       SELECT CASE WHEN uid1 = ? THEN uid2 
       ELSE uid1 END AS uid 
       FROM users_friendships 
       WHERE uid1 = ? OR uid2 = ?
    ");
    $my_friends->bind_param('iii', $myid, $myid, $myid);
    $my_friends->execute();
    $my_friends_results = $my_friends->get_result();

    while($mf = $my_friends_results->fetch_array()) {

        $mfrid = $mf['uid'];
        $friendsID[] = $mfrid;

    }

I am saving them in an array, can I somehow use this to set up a query for friends of my friends?

You can mostly reuse your existing code, looping on your array of friends:

$friendsoffriendsID = array();
foreach ($friendsID as $friendID) {
    $my_friends->bind_param('iii', $friendID, $friendID, $friendID);
    $my_friends->execute();
    $my_friends_results = $my_friends->get_result();
    while($mf = $my_friends_results->fetch_array()) {
        $mfrid = $mf['uid'];
        $friendsoffriendsID[] = $mfrid;
    }
}

This will undoubtedly give you a lot of duplicates, so you may want to just get the unique values using array_unique:

$friendsoffriendsID = array_unique($friendsoffriendsID);

I have updated my answer so that it gets the friends of friends. We use the getFriends() function I made to accomplish this easily.

<?php
function getFriends($userID){
  global $mysql; # get friends of user id
  $friends = $mysql->prepare("SELECT DISTINCT uid1, uid2 FROM users_friendships WHERE uid1 = ? OR uid2 = ?");
  $friends->bind_param("ii", $userID, $userID);
  $friends->execute();
  $friends->store_result();
  if(($numFriends = $friends->num_rows) > 0){
    $friendsArr = array();
    $friends->bind_result($friendID1, $friendID2);
    while($friends->fetch()){
     if($friendID1 == $userID && !in_array($friendID2, $friendsArr)){
      $friendsArr[] = $friendID2;
     } else if($friendID2 == $userID && !in_array($friendID1, $friendsArr)){
      $friendsArr[] = $friendID1;
     }
    }
    $friends->close();
    return $friendsArr; 
  } else { 
    return false;
  }
} // function end

# $myFriends used to store original friendIDs of you, plus all your friend's friends after. Only uniques found... 
if(($myFriends = getFriends($myid)) !== false){ // if array has friend ids, get their friend ids 
  $numFriends = count($myFriends);
  $otherFriends = array(); // array for friends of friends
  for($loopFriends = 0; $loopFriends < $numFriends; $loopFriends++){
    if(($theirFriends = getFriends($myFriends[$loopFriends])) !== false){ # their friend has friends... 
      $numTheir = count($theirFriends);
      for($loopTheir = 0; $loopTheir < $numTheir; $loopTheir++){  // loop through their friends 
        if(!in_array($theirFriends[$loopTheir], $otherFriends) !== false){ // friend id NOT in other friends
          $otherFriends[] = $theirFriends[$loopTheir]; // add their friend to otherfriends 
        }
      } // loop through their friends
    } // their friend has friends
  } // loop my friends

  $numOthers = count($otherFriends);
  for($loopOthers = 0; $loopOthers < $otherFriends; $loopOthers++){
    if(!in_array($otherFriends[$loopOthers], $myFriends) !== false){
      $myFriends[] = $otherFriends[$loopOthers]; # add their friend to the array of all friends
    }
  }

} else {
  // you have no friends 
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM