Removing Bad Words from Posts

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.

So a real short tutorial now. To remove certain words from a post (for example, bad language), you can just use these lines:

$body = "This is my post";

//Words to remove
$fowlWords = array("hello", "goodbye", "dogs", "cats");

//This replaces all occurrences of the above words with '*****'
$body = preg_replace('/\b('.implode('|',$fowlWords).')\b/','*****',$body);

In this example, if you input:

“hello everyone I love dogs”

It will be output as:

“***** everyone I love *****”

You can put this in your submit posts function just before you insert the post into the database 🙂

Check out my Facebook page for more tutorials, tips and tricks as well as information about upcoming courses! 

Friends List Page

Result with style

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:
Result with no style

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! 🙂
Result with style

Check out my Facebook page for more tutorials, tips and tricks as well as information about upcoming courses! 

Canceling a Friend Request

This post is for my social network course and is assuming you have reached the relevant section. If you haven’t yet enrolled in the course, click here

So one feature that hasn’t been implemented until now is the ability to cancel friend requests. For example, if I accidentally send someone a friend request, I have no way of cancelling it. This is actually really easy to fix and essentially all we need to do when they click the cancel request button is remove the request from the ‘friend_requests’ table.

Open profile.php and find the line of code which outputs our ‘Request Sent’ button. It should look like this:

else if ($logged_in_user_obj->didSendRequest($username)) {  
    echo '<input type="submit" name="" class="default" value="Request Sent">';  
}

Give the name attribute a value such as name=”cancel_request”. We need this name attribute so that we can handle the button press.

 
else if ($logged_in_user_obj->didSendRequest($username)) {
    echo '<input type="submit" name="cancel_request" class="default" value="Request Sent">';
}

Next, add the handler for the button at the top of profile.php (underneath all the other handlers):

if(isset($_POST['remove_friend'])) {
    $user = new User($con, $userLoggedIn);
    $user->removeFriend($username);
}

if(isset($_POST['add_friend'])) {
    $user = new User($con, $userLoggedIn);
    $user->sendRequest($username);
}

if(isset($_POST['respond_request'])) {
    header("Location: requests.php");
}

if(isset($_POST['cancel_request'])) {
    $user = new User($con, $userLoggedIn);
    $user->cancelRequest($username);
}

What this does, is when the cancel_request button is pressed, it creates an instance of the user object and then calls the ‘cancelRequest’ function (which we haven’t created yet). This function will take a parameter which is the username of the person that you sent the request to (AKA the username of the profile you are currently on). Scroll to the top of profile.php and you will see us create this $username variable.

And finally, add the cancelRequest function to the User.php class:

public function cancelRequest($user_to) { 
    $user_from = $this->user['username']; 
    mysqli_query($this->con, "DELETE FROM friend_requests WHERE user_to='$user_to' AND user_from='$user_from'"); 
}

All this function does is delete from the requests table where the user the request was sent to is the profile user (which we passed into the function) and the person who sent the request is us.

That’s it! That’s all there is too it! 🙂

Reece