The module that shows a user’s friends and followers is Example 21-10, friends.php. This interrogates the friends
table just like the members.php program, but only for a single
user. It then shows all of that user’s mutual friends and followers, along
with the people he is following.
All the followers are saved into an array called $followers
and all the people being followed are
placed in an array called $following
.
Then a neat piece of code is used to extract all those that are both
following and followed by the user, like this:
$mutual = array_intersect($followers, $following);
The array_intersect
function
extracts all members common to both arrays and returns a new array
containing only those people. This array is then stored in $mutual
. Now it’s possible to use the array_diff
function for each of the $followers
and $following
arrays to keep only those people who
are not mutual friends, like this:
$followers = array_diff($followers, $mutual); $following = array_diff($following, $mutual);
This results in the array $mutual
containing only mutual friends, $followers
containing only followers (and no
mutual friends), and $following
containing only people being followed (and no mutual friends).
Armed with these arrays, it’s a simple matter to separately display
each category of members, as can be seen in Figure 21-6. The PHP sizeof
function returns the number of elements
in an array; here I use it just to trigger code when the size is nonzero
(that is, when friends of that type exist). Note how, by using the
variables $name1
, $name2
, and $name3
in the relevant places, the code can tell
when you (the user) are looking at your own friends list, so the site
displays the words Your and You
are, instead of simply displaying the username. The commented
line can be uncommented if you wish to display the user’s profile
information on this screen.
<?php // friends.php include_once 'header.php'; if (!$loggedin) die(); if (isset($_GET['view'])) $view = sanitizeString($_GET['view']); else $view = $user; if ($view == $user) { $name1 = $name2 = "Your"; $name3 = "You are"; } else { $name1 = "<a href='members.php?view=$view'>$view</a>'s"; $name2 = "$view's"; $name3 = "$view is"; } echo "<div class='main'>"; // Uncomment this line if you wish the user's profile to show here // showProfile($view); $followers = array(); $following = array(); $result = queryMysql("SELECT * FROM friends WHERE user='$view'"); $num = mysql_num_rows($result); for ($j = 0 ; $j < $num ; ++$j) { $row = mysql_fetch_row($result); $followers[$j] = $row[1]; } $result = queryMysql("SELECT * FROM friends WHERE friend='$view'"); $num = mysql_num_rows($result); for ($j = 0 ; $j < $num ; ++$j) { $row = mysql_fetch_row($result); $following[$j] = $row[0]; } $mutual = array_intersect($followers, $following); $followers = array_diff($followers, $mutual); $following = array_diff($following, $mutual); $friends = FALSE; if (sizeof($mutual)) { echo "<span class='subhead'>$name2 mutual friends</span><ul>"; foreach($mutual as $friend) echo "<li><a href='members.php?view=$friend'>$friend</a>"; echo "</ul>"; $friends = TRUE; } if (sizeof($followers)) { echo "< span class='subhead'>$name2 followers</span><ul>"; foreach($followers as $friend) echo "<li><a href='members.php?view=$friend'>$friend</a>"; echo "</ul>"; $friends = TRUE; } if (sizeof($following)) { echo "< span class='subhead'>$name3 following</span><ul>"; foreach($following as $friend) echo "<li><a href='members.php?view=$friend'>$friend</a>"; echo "</ul>"; $friends = TRUE; } if (!$friends) echo "<br />You don't have any friends yet.<br /><br />"; echo "<a class='button' href='messages.php?view=$view'>" . "View $name2 messages</a>"; ?> </div><br /></body></html>