Social Networking Site: Adding a Delete control for Comment and Sub comment

This tutorial, is a continuation of our previous topic called Social Networking Site: Adding a Sub-comment to a specific Post. But this time, I’m going to show you how to add a delete command of comment and sub comment. To start with this course, open our philsocial folder from the document root and open the home.php file. And inside the well class above all of the codes for comment and sub-comment well creates a function called “date_toText”. And add the following code: The code below is a function that accept a parameter for date and this function will convert the date format into text so that this could provide a looking date and time for the visitors.
  1. function date_toText($datetime=""){
  2. $nicetime = strtotime($datetime);
  3. return strftime("%B %d, %Y at %I:%M %p", $nicetime);
  4. }
The output before without applying this function:
  1. 2013-12-11 09:25:31
The output after applying this function:
  1. December 11, 2013 at 09:48 AM
And the new code now is look like as shown below:
  1. echo '<br/><div style="font-size: 0.9em" ><p align="left">'.$comm->content. '</p><a>comment </a>'.date_toText($comm->created).'</div>';
  2. echo '</td>';
At this time, under the code above, we will add the following code: This code below will be used to display a glyphicon remove and it is linked to delete_post.php file. That is well creating this PHP file after this code added to home.php. And this code below we will do this into sub-comment. But in sub-comment we will process the posted data into delete_sub.php file.
  1. echo '<td><a href="delete_post.php?id='.$comm->id.'"><span class="glyphicon glyphicon-remove"></span></a></td>';
The result of this new is look like as shown below: At this time, we’re going to create a new PHP file we mentioned while ago the delete_post.php. Then add the following code:
  1. <?php
  2. require_once("includes/initialize.php");
  3. //set the delete statement statement specifying the id based on ID
  4. //pass through URL thats why we use the $_GET['id'] variable
  5. $mydb->setQuery("DELETE FROM comments where id=".$_GET['id']);
  6. //we execute the query
  7. $mydb->executeQuery();
  8. //we check if the affected rows during the deletion of data is equal to one
  9. //meaning we succesfully delete the selected comment or post.
  10. if($mydb->affected_rows() == 1){
  11.  
  12. echo "<script type=\"text/javascript\">
  13. //alert(\"Comment Successfully deleted!.\");
  14. window.location='home.php';
  15. </script>";
  16. }else{
  17. echo "<script type=\"text/javascript\">
  18. //alert(\"Comment deleted failed!.\");
  19. window.location='home.php';
  20. </script>";
  21.  
  22. }
  23. ?>
And here’s the code for delete_sub.php file.
  1. <?php
  2. require_once("includes/initialize.php");
  3. $mydb->setQuery("DELETE FROM subcomment where subc_id=".$_GET['id']);
  4. $mydb->executeQuery();
  5. if($mydb->affected_rows() == 1){
  6.  
  7. echo "<script type=\"text/javascript\">
  8. //alert(\"Comment Successfully deleted!.\");
  9. window.location='home.php';
  10. </script>";
  11. }else{
  12. echo "<script type=\"text/javascript\">
  13. //alert(\"Comment deleted failed!.\");
  14. window.location='home.php';
  15. </script>";
  16.  
  17. }
  18. ?>
And here’s all the code for home.php.
  1. <?php
  2. require_once("includes/initialize.php");
  3. ?>
  4. <!DOCTYPE html>
  5. <html lang="en">
  6. <head>
  7. <meta charset="utf-8">
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9. <meta name="description" content="">
  10. <meta name="author" content="">
  11. <link rel="shortcut icon" href="">
  12.  
  13. <title>Philsocial</title>
  14.  
  15. <!-- Bootstrap core CSS -->
  16. <link href="css/bootstrap.css" rel="stylesheet">
  17.  
  18. <!-- Custom styles for this template -->
  19. <link href="jumbotron.css" rel="stylesheet">
  20.  
  21. <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  22. <!--[if lt IE 9]>
  23. <script src="../../assets/js/html5shiv.js"></script>
  24. <script src="../../assets/js/respond.min.js"></script>
  25. <![endif]-->
  26. <?php
  27. //login confirmation
  28. confirm_logged_in();
  29. ?>
  30. .table th, .table td {
  31. border-top: none;
  32. }
  33. </head>
  34.  
  35. <body>
  36.  
  37. <div class="navbar navbar-inverse navbar-fixed-top">
  38. <div class="container">
  39. <div class="navbar-header">
  40. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
  41. <span class="icon-bar"></span>
  42. <span class="icon-bar"></span>
  43. <span class="icon-bar"></span>
  44. </button>
  45. <a class="navbar-brand" href="home.php"><B>Philsocial</B></a>
  46. </div>
  47. <form class="navbar-form navbar-left">
  48. <div class="form-group">
  49. <div class="rows">
  50. <input type="text" placeholder="Email" class="form-control" size="40">
  51. </div>
  52. </div>
  53. </form>
  54. <div class="navbar-collapse collapse">
  55.  
  56. <form class="navbar-form navbar-right">
  57. <ul class="nav navbar-nav">
  58. <li class="active"><a href="home.php">Home</a></li>
  59. <li class="dropdown">
  60.  
  61. <a href="#" class="dropdown-toggle" data-toggle="dropdown">
  62. <?php
  63. //retrieve session variable
  64. echo $_SESSION['fName'];?>
  65. <b class="caret"></b>
  66. </a>
  67.  
  68. <ul class="dropdown-menu">
  69. <li><a href="#">My Profile</a></li>
  70. <li><a href="#">Edit profile</a></li>
  71. <li><a href="#">Edit profile Picture</a></li>
  72. <li><a href="#">Customize profile</a></li>
  73. <li><a href="#">Edit Work and Education</a></li>
  74.  
  75. </ul>
  76. </li>
  77. <li class="dropdown">
  78.  
  79. <a href="#" class="dropdown-toggle" data-toggle="dropdown">Account<b class="caret"></b></a>
  80.  
  81. <ul class="dropdown-menu">
  82. <li><a href="#">Account Settings</a></li>
  83. <li><a href="#">Privacy Settings</a></li>
  84. <li><a href="#">Manage Social Accounts</a></li>
  85. <li><a href="#">Manage Credits</a></li>
  86. <li><a href="logout.php">Logout</a></li>
  87.  
  88. </ul>
  89. </li>
  90. </ul>
  91. </form>
  92. </div><!--/.navbar-collapse -->
  93. </div>
  94. </div>
  95. <div class="container">
  96. <div class="well">
  97.  
  98. <div class="row">
  99. <div class="col-xs-6 col-md-2">
  100. <a data-target="#myModal" data-toggle="modal" href="" title=
  101. "Click here to Change Image.">
  102. <?php
  103.  
  104. $mydb->setQuery("SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}' and pri='yes'");
  105. $cur = $mydb->loadResultList();
  106. if ($mydb->affected_rows()== 0){
  107. echo '<img src="./uploads/p.jpg" class="img-thumbnail" width="200px" height="100px" />';
  108.  
  109. }
  110. foreach($cur as $object){
  111.  
  112. echo '<img src="./uploads/'. $object->filename.'" class="img-thumbnail" width="200px" height="100px" />';
  113.  
  114. }
  115. ?>
  116. </a>
  117.  
  118. <!-- Modal -->
  119. <div class="modal fade" id="myModal" tabindex="-1">
  120. <div class="modal-dialog">
  121. <div class="modal-content">
  122. <div class="modal-header">
  123. <button class="close" data-dismiss="modal" type=
  124. "button">×</button>
  125.  
  126. <h4 class="modal-title" id="myModalLabel">Choose Your best
  127. picture for your Profile.</h4>
  128. </div>
  129.  
  130. <form action="save_photo.php" enctype="multipart/form-data" method=
  131. "post">
  132. <div class="modal-body">
  133. <div class="form-group">
  134. <div class="rows">
  135. <div class="col-md-12">
  136. <div class="rows">
  137. <div class="col-md-8">
  138. <input name="MAX_FILE_SIZE" type=
  139. "hidden" value="1000000"> <input id=
  140. "upload_file" name="upload_file" type=
  141. "file">
  142. </div>
  143.  
  144. <div class="col-md-4"></div>
  145. </div>
  146. </div>
  147. </div>
  148. </div>
  149. </div>
  150.  
  151. <div class="modal-footer">
  152. <button class="btn btn-default" data-dismiss="modal" type=
  153. "button">Close</button> <button class="btn btn-primary"
  154. name="savephoto" type="submit">Save Photo</button>
  155. </div>
  156. </form>
  157. </div><!-- /.modal-content -->
  158. </div><!-- /.modal-dialog -->
  159. </div><!-- /.modal -->
  160. </div>
  161.  
  162. <div class="col-xs-12 col-sm-6 col-md-8">
  163. <div class="page-header">
  164. <h3><?php echo $_SESSION['fName']. ' '. $_SESSION['lName'];?></h3>
  165. </div>
  166.  
  167. <ul class="nav nav-tabs">
  168. <li class="active">
  169. <a href="#">Wall</a>
  170. </li>
  171.  
  172. <li>
  173. <a href="info.php">Info</a>
  174. </li>
  175.  
  176. <li>
  177. <a href="message.php">Messages</a>
  178. </li>
  179. </ul>
  180. <div class="well">
  181. <div class="row">
  182. <div class="col-md-12">
  183. <div class="panel-group" id="accordion">
  184. <div class="panel panel-primary">
  185. <div class="panel-heading">
  186. <h5 class="panel-title">
  187. <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" title="What's on your mind?">
  188. Update Status
  189. </a>
  190. </h5>
  191. </div>
  192.  
  193. <form action="save_post.php" method="POST">
  194. <div id="collapseOne" class="panel-collapse collapse">
  195. <div class="panel-body">
  196. <input type="hidden" name="comment_id" value="<?php echo $_SESSION['member_id']; ?>">
  197. <input type="hidden" name="author" value="<?php echo $_SESSION['fName']. ' '. $_SESSION['lName']; ?>">
  198. <input type="hidden" name="to" value="<?php echo $_SESSION['member_id']; ?>">
  199. <textarea class="form-control" name="content" placeholder="What's on your mind?"></textarea>
  200.  
  201. </div>
  202. <div class="panel-footer" align="right">
  203. <button class="btn btn-primary btn-sm" type="submit" name="share">Share</button>
  204. </div>
  205. </div>
  206. </form>
  207. </div>
  208. </div>
  209.  
  210. <?php
  211. function date_toText($datetime=""){
  212. $nicetime = strtotime($datetime);
  213. return strftime("%B %d, %Y at %I:%M %p", $nicetime);
  214.  
  215. }
  216. global $mydb;
  217. $mydb->setQuery("SELECT * from comments where comment_id=".$_SESSION['member_id']." ORDER BY created DESC");
  218. $cur = $mydb->loadResultList();
  219.  
  220. echo '<div class="table-responsive">';
  221.  
  222.  
  223. echo '<table border="0" class="table table-hover" >';
  224.  
  225. echo '<tr>';
  226. foreach ($cur as $comm){
  227. $mydb->setQuery("SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}' and pri='yes'");
  228. $propic = $mydb->loadResultList();
  229. if ($mydb->affected_rows()== 0){
  230. echo '<td rowspan="2"><img src="./uploads/p.jpg" class="img-object" width="50px" height=60px" /></td>';
  231.  
  232. }
  233. foreach ($propic as $obj){
  234. echo '<td rowspan="2">';
  235. echo '<img src="./uploads/'. $obj->filename.'" class="img-object" width="50px" height="60px" />';
  236. echo '</td>';
  237. }
  238.  
  239. echo '<td><strong><a href="home.php?id='.$_SESSION['member_id'].'">'.$comm->author.'</a></strong>';
  240.  
  241. echo '<br/><div style="font-size: 0.9em" ><p align="left">'.$comm->content. '</p><a>comment </a>'.date_toText($comm->created).'</div>';
  242. echo '</td>';
  243. echo '<td><a href="delete_post.php?id='.$comm->id.'"><span class="glyphicon glyphicon-remove"></span></a></td>';
  244. echo '</tr>';
  245.  
  246. echo '<tr>';
  247.  
  248. echo '<td>';
  249. echo '<table border="0">';
  250.  
  251. /* this area is for listing of sub-comment*/
  252. $mydb->setQuery("SELECT * FROM `subcomment` WHERE `comment_id` = ".$comm->id );
  253. $sub = $mydb->loadResultList();
  254. foreach ($sub as $subcomm){
  255. echo '<tr>';
  256.  
  257. $mydb->setQuery("SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}' and pri='yes'");
  258. $propic = $mydb->loadResultList();
  259. if ($mydb->affected_rows()== 0){
  260. echo '<td><img src="./uploads/p.jpg" class="img-object" width="30px" height=40px" /></td>';
  261.  
  262. }
  263. foreach ($propic as $obj){
  264. echo '<td >';
  265. echo '<img src="./uploads/'. $obj->filename.'" class="img-object" width="30px" height="40px" />';
  266. echo '</td>';
  267. }
  268.  
  269. echo '<td><p><a href="home.php?id='.$_SESSION['member_id'].'">
  270. '.$comm->author.'</a>'.$subcomm->subcontent .'</p><div style="font-size: 0.9em"><p>'.date_toText($subcomm->created).'</p> </div></td>';
  271. echo '<td><a href="delete_sub.php?id='.$subcomm->subc_id.'"><span class="glyphicon glyphicon-remove"></span></a></td>';
  272. echo '<tr>';
  273. echo '</tr>';
  274. echo '</tr>';
  275.  
  276. }
  277.  
  278. //This area is for creating a new comment
  279.  
  280. echo '<tr>';
  281. echo '<form action="save_subcomm.php" method="post">';
  282. echo '<input name="commentid" type="hidden" value="'. $comm->id .'">';
  283. echo '<input name="subauthor" type="hidden" value="'. $_SESSION['fName']. ' '. $_SESSION['lName'] .'">';
  284.  
  285. $mydb->setQuery("SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}' and pri='yes'");
  286. $propic = $mydb->loadResultList();
  287. if ($mydb->affected_rows()== 0){
  288. echo '<td><img src="./uploads/p.jpg" class="img-object" width="30px" height=30px" /></td>';
  289.  
  290. }
  291. foreach ($propic as $obj){
  292. echo '<td >';
  293. echo '<img src="./uploads/'. $obj->filename.'" class="img-object" width="30px" height="30px" />';
  294. echo '</td>';
  295. }
  296.  
  297. echo '<td><input name="subcontent" type="text" style="width: 400px;" class="form-control input-sm" placeholder="Write a comment...">';
  298. echo '</form>';
  299. echo '</tr>';
  300. echo '</table>';
  301. //echo '</div>';
  302. /*
  303. End of New sub comment.
  304. */
  305. echo '</div>';
  306.  
  307. echo '</div>';//end of col-lg-6
  308. echo '</div>';//end of row
  309. echo '</div>';//end of well
  310. echo '</tr>';
  311.  
  312.  
  313. }
  314. echo '</table>';
  315. ?>
  316.  
  317. </div>
  318.  
  319. </div>
  320. </div>
  321. </div>
  322. </div>
  323.  
  324.  
  325.  
  326. </body>
  327. </html>
  328.  
  329. <hr>
  330.  
  331. </div> <!-- /container -->
  332. <footer>
  333. <p align="center">&copy; Philsocial 2013</p>
  334. </footer>
  335. <!-- Bootstrap core JavaScript
  336. ================================================== -->
  337. <!-- Placed at the end of the document so the pages load faster -->
  338. <script src="js/tooltip.js"></script>
  339. <script src="assets/js/jquery.js"></script>
  340. <script src="js/bootstrap.min.js"></script>
  341. <script src="js/popover.js"></script>
  342.  
  343.  
  344. </body>
  345. </html>

Comments

Submitted bybouman4on Wed, 06/26/2019 - 04:25

i,m searching for this and a wall without pictures

Add new comment