showing list of friends

46 Views Asked by At

I'm creating a friend system for a basic social network. I've created the below function to display all the friends of a user. Legend: sid = sender id; rid = receiver id;

public function friendsList()
    {
        $id = $_SESSION['user'];
    $status = 1;

    $sql = $this->_db->prepare("SELECT * FROM requests WHERE (sid = :k OR rid = :m) AND status = :n");
    $sql->bindValue(':k', $id);
    $sql->bindValue(':m',$id);
    $sql->bindValue(':n', $status);
    $sql->execute(); 

    while($row = $sql->fetchAll(PDO::FETCH_ASSOC)){
      $rsize = count($row);
      $a = $_SESSION['user'];
      $rel = new User();
      $sel = new User();
      for($i=0; $i<$rsize; $i++)
        {
            if($a === $row[$i]['sid']){
              $friendid = $row[$i]['rid'];
              $rel = $rel->getUser($friendid);
              return $rel;
            }

            if($a === $row[$i]['rid']){
              $friendid = $row[$i]['sid'];
              $sel = $sel->getUser($friendid);
              return $sel;
            }
        }
     }
  }

The getUser() function is working fine.All it does is return the username of the respective friend's ID.

My problem is this: When i execute the code, the id of the current user is being assigned to only either one of the two if loops, which returns only friends from a single instance, such as, where the current user is the sender or vice versa.

So, how do i change it in such a way that i can return friends when the current user was both the sender and receiver?? kindly help !!

0

There are 0 best solutions below