Hi,
I have created a system where users can follow another user, I am currently querying the table to display who is following who.
I have two queries as shown below:
// followers query
$query = "SELECT * FROM friends WHERE friFriend='$view'";
$result = queryMysql($query);
$num = mysql_num_rows($result);
// for statement to store all results for $result into the array $followers
for ($j = 0 ; $j < $num ; ++$j)
{
$row1 = mysql_fetch_row($result);
$followers[$j] = $row;
}
// following query
$query = "SELECT * FROM friends WHERE friUser='$view'";
$result = queryMysql($query);
$num = mysql_num_rows($result);
// for statement to store all results for $result into the array $following
for ($j = 0 ; $j < $num ; ++$j)
{
$row = mysql_fetch_row($result);
$following[$j] = $row;
}
The queries are dependant on who a variable called $view which is the profile that is being used. for example if a user views John's profile then $view = John.
The first query selects everyone that is following $view or John and the second query shows who $view or John is following, and as you can see they are stored in the corresponding arrays $followers and $following.
Then I create a thrid array for mutual followings and exclude the mutual followings from $followers and $following as shown below
$mutual = array_intersect($followers, $following); // mutual is the matching results of $followers and $following
$followers = array_diff($row1, $mutual); // $followers is the difference between $followers and $mutual
$following = array_diff($following, $mutual); // $following is the difference between $followers and $mutual
Once that is all done I should have 3 functioning arrays which store $view's or John's mutual followings, their followers and who they are following.
Now it comes to displaying them here is the code for display (I have only displayed it for 1 array as they are all the same)
// if followers has results in it
if (sizeof($followers))
{
echo "<b>$name2 followers</b>";
$p = 0;
// echo out a link to following friend
while($followers[$p] <= $p)
{
echo $followers[$p];
$followers[$p] = $name;
echo $name;
echo "<a href='profile.php?view=$name'>$name</a>";
echo "<br />";
echo "<a href='profile.php?view=" . $view . "&add=". $view . "'> Follow</a>";
echo "<br />";
$friends = TRUE;
$p++;
}
}
This is where it goes wrong, the intention is to check that $followers has a value in it, if so, proceed. That part (the if statement) works fine, its the while statement that does not work, which if working correctly should display the following users name as a link which comes from the variable $name.
I have tried many different ways of doing this and I cannot work it out, I have also tried to use foreach statements and I could still not get this working.
I really hope someone can help.
Thanks alot.