This post is for my social network course and is assuming you have enrolled and reached the relevant section. If you haven’t yet enrolled in the course, click here to enrol.
I’ve had a lot of requests for a friends list page. So, here it is! 🙂
We will start by creating a function which will return an array of the users friends. In User.php, create the following function:
public function getFriendsList() { $friend_array_string = $this->user['friend_array']; //Get friend array string from table $friend_array_string = trim($friend_array_string, ","); //Remove first and last comma return explode(",", $friend_array_string); //Split to array at each comma }
Next, create a new file called friends.php on that page, copy and paste the following code:
<?php include("includes/header.php"); //Get username parameter from url if(isset($_GET['username'])) { $username = $_GET['username']; } else { $username = $userLoggedIn; //If no username set in url, use user logged in instead } ?> <div class="main_column column" id="main_column"> </div>
What we’ve done here set up an empty page which takes a username parameter in the url e.g. friends.php?username=reece_kenney
Next we are going to add the code which calls the getFriendsList function we created above. Inside of the
div, add the following code:
<?php $user_obj = new User($con, $username); foreach($user_obj->getFriendsList() as $friend) { $friend_obj = new User($con, $friend); echo "<a href='$friend'> <img class='profilePicSmall' src='" . $friend_obj->getProfilePic() ."'>" . $friend_obj->getFirstAndLastName() . "</a> <br>"; } ?>
Refresh the page and you should see a list of the users friend along with their profile image:
It doesn’t look great, so just add the following code to your css file:
img.profilePicSmall { height: 40px; width: 40px; border-radius: 25px; margin: 5px; }
Now it should look a little better! 🙂