can I make this select follower/following script more organized? (PHP/Mysql)

Posted by ggfan on Stack Overflow See other posts from Stack Overflow or by ggfan
Published on 2010-05-31T19:59:43Z Indexed on 2010/05/31 20:03 UTC
Read the original article Hit count: 431

Filed under:
|

In this script, it gets the followers of a user and the people the user is following. Is there a better relationship/database structure to get a user's followers and who they are following? All I have right now is 2 columns to determine their relationship. I feel this is "too simple"?

(MYSQL)

USER     |   FRIEND
avian         gary
cend          gary
gary          avian
mike          gary       



(PHP)
    $followers = array();
    $followings = array();

$view = $_SESSION['user']; //view is the person logged in
$query = "SELECT * FROM friends WHERE user='$view'";
$result = $db->query($query);
while($row=$result->fetch_array())
{
    $follower=$row['friend'];
    $followers[] = $follower;
}
print_r($followers);
echo "<br/>";

$query2 = "SELECT * FROM friends WHERE friend='$view'";
$result2 = $db->query($query2);
while($row2=$result2->fetch_array())
{
    $following=$row2['user'];
    $followings[] = $following;
}
print_r($followings);
echo "<br/>";

$mutual = array_intersect($followers, $followings);
print_r($mutual);


**DISPLAY**
Your mutual friends 
    avian

Your followers
    avian

You are following
  avian
  cen
  mike

(I know avian is in all 3 displays, but I want to keep it that way)

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql