Ben bu seçkin takipçisi / Aşağıdaki komut daha organize hale getirebiliriz?

2 Cevap php

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

(Ben kuş 3 görüntüler olduğunu biliyorum, ama ben bu şekilde devam etmek istiyorum)

2 Cevap

Çalıştığını ve bunun performans sorunlarına neden yoksa, onu tutmak. 'Çok basit' çalışan bir şey değiştirmek için iyi bir neden değildir.

Sen bir sorgu yapabilirsiniz:

"SELECT * FROM friends WHERE user = '$view' OR friend = '$view';"

o zaman

while($row = $result->fetch_array())
{
    $destination = ($row['user'] == $view) ? &$followers : &$followings;
    $otherUser = ($row['user'] == $view) ? $row['friend'] : $row['user'];
    $destination[] = $otherUser;
}

Bu daha az kod, ama onun 'akıllılık' o sert ne oluyor takip etmek için yapar. Sevgiler takip etmek daha kolay, ve o şey için sayar.