Forum Tutorial - Deleting Posts
Submitted by Yorkiebar on Monday, March 3, 2014 - 06:10.
Introduction:
This tutorial is on how to add the ability for a user to delete their posts.
The Theory:
We could add a user panel where it lists all the users posts along with options to delete them, but instead we are going to add a delete option adjacent to the post itself to save space - plus I'm planning on making an admin panel tutorial soon.
The PHP:
So in the part of the PHP where we actually output each reply to a thread, we want to check if the logged in users' username held in the session variable is equal to the author of the post, if it is then we add an option to delete the reply...
We do a similar thing for the threads but in a slightly different place to make the layout more clear...
Deleting The Posts:
Finally we need the actual PHP script to delete the given post. We add a link around each delete option to the current page with "?act=delete&type=" followed by the type of post (thread or reply) then "&id=" followed by the post ID so that when the delete button is clicked it links to the given page with the correct parameters through PHP GET statements. So now we need to check if the act parameter is set to delete, and if it is, we delete the post from the table with the appropriate ID from the appropriate table...
- $replies .= '<tr><td>'.$row["content"].'</td><td>'.$author.'</td><td>'.$repliedUser["signature"].'</td>';
- $replies .= '<td><a href="threadPage.php?act=delete&type=reply&id='.$row["id"].'">Delete</a></td>';
- $replies .= '</tr>';
- echo '<a href="threadPage.php?act=delete&type=thread&id='.$id.'">Delete Thread</a>';
- echo "
- <h3>";
- $all = $info['rating'];
- $total = $info['totalRatings'];
- if ($all == 0 || $all == null || $total == 0 || $total == null) {
- echo 'No ratings have yet been given for this thread.';
- }else {
- $average = $all / $total;
- echo 'Average Rating: '.$average;
- }
- }
- echo "</h3>
- $delq;
- $delAuthor;
- $delID = $_GET['id'];
- $delType = $_GET['type'];
- if ($delType == 'reply') {
- $delAuthor = $delAuthorInfo['author'];
- }else
- echo 'You do not have permission to do that!';
- }else if ($delType == 'thread') {
- $delAuthor = $delAuthorInfo['author'];
- }else
- echo 'You do not have permission to do that!';
- }
- if ($delq) {
- echo 'Deleted post successfully.';
- if ($delType == 'thread') {
- }
- }else
- echo 'Failed to delete post.';
- }
- }
Add new comment
- 40 views