Deleting a User’s Posts


As another extra feature of the site I wanted the user to be able to delete their post so they could either redo it or be able to update it. I had to add a new delete_post.php file and create an if statement so if the user was logged in then the delete button would appear and they were able to delete only their posts. I also created a message if they were not able to delete their post then it would appear with an error message.

Screen Shot 2015-04-19 at 16.49.17

I then needed to add the function to the box1.php as that is where I wanted the button to appear.

<!--?php if ($_SESSION["user_id"]==$row["user_id"]) {?--></pre>
<div id="delete_post" class="delete"><a id="" href="delete_post.php?id=<?php echo $row[">"><button class="btn btn-danger" type="button">Delete Post</button></a></div>
<pre><!--?php } ?-->
<!--?php } else { ?-->
    <!--?php } ?-->

I then wanted to style this button so the text would stand out in  the box and it actually looked like a delete button. I went onto Bootstrap and found the best delete button (the danger button) I could find and added it to the box1.php code.

Screen Shot 2015-04-19 at 16.53.58

This is what the final product looks like:

Screen Shot 2015-04-19 at 17.00.52




This project was very reliant on teamwork. Amy Wichelow and I had never really done much coding before let alone PHP! This meant that we had to work together a lot to get the work done and to be able to understand what the code did.

We spent a lot of time in the labs, both working on the same computer trying to fix errors. Amy seemed to understand the PHP more than I had done so she took me through the code and helped me understand this. I did research on another elements we could add to the site like the timestamp and trying to let a user delete a post. I had some prior knowledge on HTML and CSS so I did a lot of the styling and in the end ended up designing the whole site.

Amy and I communicated a lot outside of the labs if we were trying to get some code to work. I asked for my group’s opinions of the design.

Amy and I tried to stay organised by creating to-do lists on the elements and parts of the coding we still had to do. I took charge of uploading the project to GitHub and installing the Wiki onto the server.

Overall Amy and I worked well as a team and I feel that if we did not work as well we may have not finished the project to the deadline, as we spent a lot of time together trying to fix the code. We weren’t afraid to ask Kyle if after a while of trying to fix an error we were still stuck which helped us understand PHP and PHPMyAdmin in a lot more depth.



I decided that I would try and add a timestamp to the post that come up on the homepage. To do this I needed to add a timestamp row on PHPMyAdmin so it would record the time that the post was submitted. Adding this feature would prove to be  an aesthetic element to the posts and also meant that sorting the post by most recent would be easier to do.

This is a video someone recommened me to watch that gave me the details to add a timestanmp onto the post:

I used this video to understand what code to write and how it worked. I ended up changing the layout of the timestamp so that It said “3 hours ago” or “2 minutes ago”.

Firstly I had to create the function which would cause the time to be posted in the post box. This code also included the way that the timestamp would be printed and named the function as “$timestamp”. The code below is the code I wrote for the functions.php file.

class convertToAgo {
    function convert_datetime($str) {
        //sets the timezone to UTC
        //explode breaks a string into an array
   		list($date, $time) = explode(' ', $str);
    	list($year, $month, $day) = explode('-', $date);
    	list($hour, $minute, $second) = explode(':', $time);
    	$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
    	return $timestamp;
        //this function converts into a timestamp in the "2015-01-01 12:12:12" format
    function makeAgo($timestamp){
   		$difference = time() - $timestamp;
   		$periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
   		$lengths = array("60","60","24","7","4.35","12","10");
   		for($x = 0; $difference >= $lengths[$x]; $x++){
   			$difference /= $lengths[$x];
   			$difference = round($difference);
   		if($difference != 1) $periods[$x].= "s";
   			$text = "$difference $periods[$x] ago";
   			return $text;
        //this function gives out the differences between the time and the timestamp-time

Screen Shot 2015-04-17 at 18.08.58

The next step was to place the function in box1.php as the timestamp needed to be shown in the box with the other profile information. This linked the database time data with the function so it could find the right information to use.

include_once("../includes/functions.php"); // Include the class library
    $timeAgoObject = new convertToAgo; // Create an object for the time conversion functions
    // Query your database here and get timestamp
    $ts = $row["dateofpost"];
    $convertedTime = ($timeAgoObject -> convert_datetime($ts)); // Convert Date Time
    $when = ($timeAgoObject -> makeAgo($convertedTime)); // Then convert to ago time
    <div class="time_ago" <h2><?php echo "".$when; ?></h2> </div>

This is the code I placed into the box1.php file. Screen Shot 2015-04-17 at 18.08.01

I then styled this timestamp in the CSS by making the font slightly opaque and giving the text a different colour.

Screen Shot 2015-04-17 at 18.19.46



For this project we needed to create 2 databases on the PHPMyAdmin server. One was needed to record usernames and passwords and the second was to record the data from the profiles.

This is the database for the usernames and passwords:

Screen Shot 2015-04-17 at 16.37.51

This was the structure for that database:

Screen Shot 2015-04-17 at 16.39.18

The second database was slightly more confusing. We had to remember to make an ID row and name all of the elements appropriately so we could accurately link this table to the form we will create using PHP. This is the second table:

Screen Shot 2015-04-17 at 16.41.25

This was the structure for this database:

Screen Shot 2015-04-17 at 16.41.37

To add this to our code we needed to create a connect.php file and link this PHPMyAdmin database to our code. This is what it looks like:

Screen Shot 2015-04-17 at 16.42.51

Below is the code from the signup.php and is where we linked our PHPMyAdmin to the code and created the form where the user would be able to submit their information and then it would save on the PHPMyAdmin and then would be echoed out on the homepage as a profile.

&lt;?php require_once(&quot;../includes/session.php&quot;); ?&gt;
&lt;?php require_once(&quot;../includes/connect.php&quot;); ?&gt;
&lt;?php require_once(&quot;../includes/functions.php&quot;); ?&gt;
&lt;?php include_once(&quot;../includes/templates/header.php&quot;); ?&gt;

&lt;a name=&quot;formlogin&quot;&gt;&lt;/a&gt;
&lt;a name=&quot;formsignup&quot;&gt;&lt;/a&gt;


    if(isset($_POST[&quot;post&quot;])) {
        $fullname = ucfirst($_POST[&quot;full_name&quot;]);
        $email = ucfirst($_POST[&quot;email&quot;]);
        $age = ucfirst($_POST[&quot;age&quot;]);
        $gender = ucfirst($_POST[&quot;gender&quot;]);
        $personalprofile = ucfirst($_POST[&quot;bio&quot;]);
        $location = ucfirst($_POST[&quot;location&quot;]);
        $user_id = $_SESSION[&quot;user_id&quot;];
        //$photoupload = ucfirst($_POST[&quot;photo_upload&quot;]);
        //$cvupload = ucfirst($_POST[&quot;cv_upload&quot;]);

        $query = &quot;INSERT INTO newpost (user_id, full_name, email, age,
 gender, bio, location) VALUES ('{$_SESSION['user_id']}', 
'{$fullname}', '{$email}', '{$age}', '{$gender}', '{$personalprofile}',
        $result = mysqli_query($connection, $query);

        if($result) {
            $_SESSION[&quot;message&quot;] = &quot;Post successful&quot;;
        } else {
            $_SESSION[&quot;message&quot;] = &quot;Something went wrong!&quot;;
    } else 

            $fullname = &quot;&quot;;
            $email = &quot;&quot;;
            $age = &quot;&quot;;
            $gender = &quot;&quot;;
            $personalprofile = &quot;&quot;;
            $location = &quot;&quot;;
            //$photoupload = &quot;&quot;;
            //$cvupload = &quot;&quot;;


&lt;div class=&quot;container&quot;&gt;
    &lt;h3 class=&quot;post&quot;&gt;New Post&lt;/h3&gt;
    &lt;div class=&quot;jumbotronlogin&quot;&gt;
	    &lt;div class=&quot;row&quot;&gt;
	        &lt;div class=&quot;col-md-12&quot;&gt;
	            &lt;div id=&quot;form-box&quot;&gt;

                &lt;form action=&quot;profile.php&quot; method=&quot;post&quot;&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
	&lt;label for=&quot;inputName&quot;&gt;Full Name&lt;/label&gt;
		&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;full_name&quot; id=&quot;exampleInputName2&quot; placeholder=&quot;Full Name&quot;&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
	&lt;label for=&quot;inputName&quot;&gt;Email Address&lt;/label&gt;
		&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;email&quot; id=&quot;exampleInputName2&quot; placeholder=&quot;Enter Your Email Address&quot;&gt;

                    &lt;div class=&quot;form-group&quot;&gt;
	&lt;label for=&quot;inputName&quot;&gt;Age&lt;/label&gt;
		&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;age&quot; id=&quot;exampleInputName2&quot; placeholder=&quot;Age&quot;&gt;

                    &lt;div class=&quot;form-group&quot;&gt;
	&lt;label for=&quot;inputName&quot;&gt;Gender&lt;/label&gt;
						&lt;select class=&quot;form-control&quot; name=&quot;gender&quot;&gt;
				            &lt;option&gt;Please Select&lt;/option&gt;
				            &lt;option value=&quot;male&quot;&gt;Male&lt;/option&gt;
				            &lt;option value=&quot;female&quot;&gt;Female&lt;/option&gt;

                    &lt;div class=&quot;form-group&quot;&gt;
	&lt;label for=&quot;inputName&quot;&gt;Personal Profile&lt;/label&gt;
                    &lt;textarea name=&quot;bio&quot; class=&quot;form-control&quot; rows=&quot;3&quot; placeholder=&quot;250 Characters&quot;&gt;&lt;/textarea&gt;

                    &lt;div class=&quot;form-group&quot;&gt;
	&lt;label for=&quot;inputName&quot;&gt;Location&lt;/label&gt;
		&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;location&quot; id=&quot;exampleInputName2&quot; placeholder=&quot;Location&quot;&gt;
                     &lt;!--&lt;div class=&quot;form-group&quot;&gt;
                        &lt;label for=&quot;exampleInputFile&quot;&gt;Photo Upload&lt;/label&gt;
                        &lt;input type=&quot;file&quot; id=&quot;exampleInputFile&quot;&gt;

                    &lt;div class=&quot;form-group&quot;&gt;
                        &lt;label for=&quot;exampleInputFile&quot;&gt;CV Upload (optional)&lt;/label&gt;
                        &lt;input type=&quot;file&quot; id=&quot;exampleInputFile&quot;&gt;
                    &lt;input type=&quot;submit&quot; name=&quot;post&quot; class=&quot;btn btn-default&quot;&gt;


&lt;?php include_once(&quot;../includes/templates/footer.php&quot;); ?&gt;

The signup would mean that a user could sign in and if the data was different to what was on the PHPMyAdmin then it would not log them in.Screen Shot 2015-04-17 at 16.43.08