Ah for the fun of it, I made a little interpretation of your project. It does NOT automatically add to follow if user views a profile, but instead has follow/unfollow links. It does show both users following the user and who the user is following. I’m sure you can find some of it useful.
<?php
//include DB connect and session start
//include("config.php");
//session_start();
// Make sure user is logged in. Adjust as needed
if(!isset($_SESSION['user_id'])){
header("location: login.php");
exit;
}
//Set user ID to variable. Adjust as needed
$user_id = $_SESSION['user_id'];
// Add following
if(isset($_GET['follow'])){
$followid = $_GET['follow'];
$sql = "INSERT INTO follow_user (`userid`,`follow_id`) VALUES (?,?)";
$query = $mysqli->prepare($sql);
$query->bind_param('ii', $user_id,$followid);
$query->execute();
}
// Remove following
if(isset($_GET['unfollow'])){
$followid = $_GET['unfollow'];
$sql = "DELETE FROM follow_user WHERE `userid` = ? AND `follow_id` = ?";
$query = $mysqli->prepare($sql);
$query->bind_param('ii', $user_id,$followid);
$query->execute();
}
// Build profiles array
$profiles = array();
if(isset($_GET['view'])){
$currentid = $_GET['view'];
$sql = "SELECT
`m`.`id`,
`m`.`username`,
`m`.`profilepic`,
`mf`.`id` AS fid,
`mf`.`username` as fusername,
`mf`.`profilepic` as fprofilepic,
`mhf`.`id` AS hfid,
`mhf`.`username` as hfusername,
`mhf`.`profilepic` as hfprofilepic
FROM `members` AS m
LEFT JOIN `follow_user` AS f
ON `f`.`follow_id` = `m`.`id`
LEFT JOIN `members` AS mf
ON `mf`.`id` = `f`.`userid`
LEFT JOIN `follow_user` AS hf
ON `hf`.`userid` = `m`.`id`
LEFT JOIN `members` AS mhf
ON `mhf`.`id` = `hf`.`follow_id`
WHERE `m`.`id` = ?";
$fol =$mysqli->prepare($sql);
$fol->bind_param('s', $currentid);
$fol->execute();
$fol->store_result();
if($fol->num_rows >= 1) {
$fol->bind_result($id,$username,$profilepic,$follow_id,$follow_username,$followpic,$hfollow_id,$hfollow_username,$hfollowpic); // get variables from result.
while ($fol->fetch()) {
$profiles[$id]['username'] = $username;
$profiles[$id]['profilepic'] = $profilepic;
$profiles[$id]['followers'][$follow_id]['username'] = $follow_username;
$profiles[$id]['followers'][$follow_id]['profilepic'] = $followpic;
$profiles[$id]['hasfollowed'][$hfollow_id]['username'] = $hfollow_username;
$profiles[$id]['hasfollowed'][$hfollow_id]['profilepic'] = $hfollowpic;
}
}
}else{
$sql = "SELECT
`m`.`id`,
`m`.`username`,
`m`.`profilepic`,
`mf`.`id` AS fid,
`mf`.`username` as fusername,
`mf`.`profilepic` as fprofilepic,
`mhf`.`id` AS hfid,
`mhf`.`username` as hfusername,
`mhf`.`profilepic` as hfprofilepic
FROM `members` AS m
LEFT JOIN `follow_user` AS f
ON `f`.`follow_id` = `m`.`id`
LEFT JOIN `members` AS mf
ON `mf`.`id` = `f`.`userid`
LEFT JOIN `follow_user` AS hf
ON `hf`.`userid` = `m`.`id`
LEFT JOIN `members` AS mhf
ON `mhf`.`id` = `hf`.`follow_id`";
$fol =$mysqli->prepare($sql);
$fol->execute();
$fol->store_result();
if($fol->num_rows >= 1) {
$fol->bind_result($id,$username,$profilepic,$follow_id,$follow_username,$followpic,$hfollow_id,$hfollow_username,$hfollowpic); // get variables from result.
while ($fol->fetch()) {
$profiles[$id]['username'] = $username;
$profiles[$id]['profilepic'] = $profilepic;
$profiles[$id]['followers'][$follow_id]['username'] = $follow_username;
$profiles[$id]['followers'][$follow_id]['profilepic'] = $followpic;
$profiles[$id]['hasfollowed'][$hfollow_id]['username'] = $hfollow_username;
$profiles[$id]['hasfollowed'][$hfollow_id]['profilepic'] = $hfollowpic;
}
}
}
//echo "<pre>";
//print_r($profiles);
//echo "</pre>";
$display = "";
$imgdir = "images";
$display .= '<div class="wrapper">'."\\r";
$display .= '<div class="title">Profile</div>'."\\r";
foreach($profiles as $id => $arry):
if(!empty($id)):
//Show Username
$display .= '<div class="name">'."\\r";
$display .= "{$arry['username']}\\r";
$display .= '</div>'."\\r";
//Show Image
$display .= '<div class="imageholder">'."\\r";
$image = (!empty($arry['profilepic']) ? "{$arry['profilepic']}" : "noimage.gif");
$display .= '<img src="' . $imgdir . '/' . $image . '" class="image" />'."\\r";
$display .= '</div>'."\\r";
//Links
$FollowLink = (array_key_exists($user_id,$arry['followers']) ? '<a href="?unfollow=' . $id . '">UnFollow</a>' : '<a href="?follow=' . $id . '">Follow</a>');
$display .= '<div class="links">'."\\r";
if(!isset($_GET['view'])):
$display .= '<a href="?view=' . $id . '">View</a>';
endif;
if($id != $user_id):
$display .= $FollowLink;
endif;
if(isset($_GET['view'])):
$display .= '<br /><a href="?">Show All</a>';
endif;
$display .= '</div>'."\\r";
if(isset($_GET['view'])):
// Followers Display
$display .= '<div class="followers">'."\\r";
$display .= '<div class="title">Followers</div>'."\\r";
foreach($arry['followers'] as $fid => $farry):
if(!empty($fid)):
//Show Username
$display .= '<div class="name">'."\\r";
$display .= "{$farry['username']}\\r";
$display .= '</div>'."\\r";
//Show Image
$display .= '<div class="imageholder">'."\\r";
$image = (!empty($farry['profilepic']) ? "{$farry['profilepic']}" : "noimage.gif");
$display .= '<img src="' . $imgdir . '/' . $image . '" class="image" />'."\\r";
$display .= '</div>'."\\r";
//Links
$FollowLink = (array_key_exists($user_id,$arry['followers']) ? '<a href="?unfollow=' . $fid . '">UnFollow</a>' : '<a href="?follow=' . $fid . '">Follow</a>');
$display .= '<div class="links">'."\\r";
if($fid != $user_id):
$display .= '<a href="?view=' . $fid . '">View</a>';
endif;
if($fid != $user_id):
$display .= $FollowLink;
endif;
$display .= '</div>'."\\r";
endif;
endforeach;
$display .= '</div>'."\\r";
// Has Followed Display
$display .= '<div class="hasfollowed">'."\\r";
$display .= '<div class="title">Has Followed</div>'."\\r";
foreach($arry['hasfollowed'] as $hfid => $hfarry):
if(!empty($hfid)):
//Show Username
$display .= '<div class="name">'."\\r";
$display .= "{$hfarry['username']}\\r";
$display .= '</div>'."\\r";
//Show Image
$display .= '<div class="imageholder">'."\\r";
$image = (!empty($hfarry['profilepic']) ? "{$hfarry['profilepic']}" : "noimage.gif");
$display .= '<img src="' . $imgdir . '/' . $image . '" class="image" />'."\\r";
$display .= '</div>'."\\r";
//Links
$FollowLink = (array_key_exists($user_id,$arry['followers']) ? '<a href="?unfollow=' . $hfid . '">UnFollow</a>' : '<a href="?follow=' . $hfid . '">Follow</a>');
$display .= '<div class="links">'."\\r";
if($hfid != $user_id):
$display .= '<a href="?view=' . $hfid . '">View</a>';
endif;
if($hfid != $user_id):
$display .= $FollowLink;
endif;
$display .= '</div>'."\\r";
endif;
endforeach;
$display .= '</div>'."\\r";
endif;
endif;
endforeach;
$display .= '</div>'."\\r";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Profile</title>
<style type="text/css">
.wrapper {
width: 450px;
overflow: hidden;
}
.title {
width: 120px;
font-size: 18px;
font-weight: bold;
text-align:center;
clear: left;
float: left;
white-space: nowrap;
}
.name {
width: 120px;
font-size: 14px;
text-align:center;
font-weight: bold;
clear: left;
float: left;
white-space: nowrap;
}
.imageholder {
width: 120px;
height: 120px;
text-align:center;
clear: left;
float: left;
}
.image {
width: 120px;
height: 120px;
}
.links {
width: 120px;
text-align:center;
clear: left;
float: left;
}
.links a{
font-size: 14px;
text-align:center;
margin: 0 auto;
padding: 0 5px;
text-decoration: none;
}
.followers {
margin-top: 30px;
margin-left: 150px;
width: 120px;
text-align:center;
overflow: hidden;
clear: left;
float: left;
}
.hasfollowed {
margin-top: 30px;
width: 120px;
text-align:center;
overflow: hidden;
clear: right;
float: right;
}
</style>
</head>
<body>
<?php if(isset($display)){ echo $display; }?>
</body>
</html>