• We just launched and are currently in beta. Join us as we build and grow the community.

Social Networking Site: Adding a Sub-comment to a Specific Post

test222

Comedy Algorithm Developer
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
143
Likes
54
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial is a continuation of our previous topic called “Facebook Wall Script Clone(What’s On Your Mind)”. But this time, we’re going to focus on how to add a sub comment in every shared post of a user. To start with this tutorial, create first a table in your database called “subcomment” and here’s the table structure of this table.

  1. CREATE

    TABLE

    IF

    NOT

    EXISTS

    `subcomment`

    (
  2. `subc_id`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `comment_id`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  4. `subauthor`

    VARCHAR

    (

    255

    )

    NOT

    NULL

    ,
  5. `subcontent`

    text NOT

    NULL

    ,
  6. `created`

    datetime NOT

    NULL

    ,
  7. PRIMARY

    KEY

    (

    `subc_id`

    )
  8. )

    ENGINE=

    MyISAM DEFAULT

    CHARSET=

    latin1 AUTO_INCREMENT

    =

    13

    ;

Then, let's open the home.php file, and on the last part of our code used for displaying the Post of every user. Or after this line of code echo '
'.$comm->content.'

';

, add the following code:

This code is used for displaying a text input for adding a new sub comment.

  1. echo

    '<table border="0">'

    ;
  2. /* this area is for listing of sub-comment*/

  3. echo

    '<tr>'

    ;
  4. echo

    '<form action="save_subcomm.php" method="post">'

    ;
  5. echo

    '<input name="commentid" type="hidden" value="'

    .

    $comm

    ->

    id

    .

    '">'

    ;
  6. echo

    '<input name="subauthor" type="hidden" value="'

    .

    $_SESSION

    [

    'fName'

    ]

    .

    ' '

    .

    $_SESSION

    [

    'lName'

    ]

    .

    '">'

    ;

  7. $mydb

    ->

    setQuery

    (

    "SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}

    '"

    )

    ;
  8. $propic

    =

    $mydb

    ->

    loadResultList

    (

    )

    ;
  9. if

    (

    $mydb

    ->

    affected_rows

    (

    )

    ==

    0

    )

    {
  10. echo

    '<td><img src="./uploads/p.jpg" class="img-object" width="30px" height=30px" /></td>'

    ;

  11. }
  12. foreach

    (

    $propic

    as

    $obj

    )

    {
  13. echo

    '<td >'

    ;
  14. echo

    '<img src="./uploads/'

    .

    $obj

    ->

    filename

    .

    '" class="img-object" width="30px" height="30px" />'

    ;
  15. echo

    '</td>'

    ;
  16. }

  17. echo

    '<td><input name="subcontent" type="text" style="width: 370px;" class="form-control" placeholder="Write a comment...">'

    ;
  18. echo

    '</form>'

    ;
  19. echo

    '</tr>'

    ;
  20. echo

    '</table>'

    ;

When you test the code above, it will look like as shown below:

sub1.png


Next, we’re going to create a new PHP file called “save_subcomm.php”. This file will process the submitted data by the user and store into the database table name subcommittee. And here’s the following code:

  1. <?php
  2. require_once

    (

    "includes/initialize.php"

    )

    ;

  3. $comment_id

    =

    $_POST

    [

    'commentid'

    ]

    ;
  4. $content

    =

    $_POST

    [

    'subcontent'

    ]

    ;
  5. $author

    =

    $_POST

    [

    'subauthor'

    ]

    ;
  6. $created

    =

    strftime

    (

    "%Y-%m-%d

    %H:%M:%S"

    ,

    time

    (

    )

    )

    ;


  7. global

    $mydb

    ;
  8. $mydb

    ->

    setQuery

    (

    "INSERT INTO `philsocialdb`.`subcomment` (`subc_id`, `comment_id`, `subauthor`, `subcontent`, `created`)
  9. VALUES (NULL, '{$comment_id}

    ', '{$author}

    ', '{$content}

    ', '{$created}

    ');"

    )

    ;
  10. $mydb

    ->

    executeQuery

    (

    )

    ;

  11. if

    (

    $mydb

    ->

    affected_rows

    (

    )

    ==

    1

    )

    {

  12. echo

    "<script type=\"

    text/javascript\"

    >
  13. //alert(\"

    );
  14. window.location='home.php';
  15. </script>"

    ;

  16. }

    else

    {
  17. echo

    "<script type=\"

    text/javascript\"

    >
  18. alert(\"

    );
  19. </script>"

    ;
  20. }


  21. ?>

Then after saving the sub comment in the database, we’re now going to display a list of a sub comment under the specific post of a user. To do this, add the following code in the area we provided under the table tag for adding a new sub comment.

  1. /* this area is for listing of sub-comment*/
  2. $mydb

    ->

    setQuery

    (

    "SELECT * FROM `subcomment` WHERE `comment_id` = "

    .

    $comm

    ->

    id

    )

    ;
  3. $sub

    =

    $mydb

    ->

    loadResultList

    (

    )

    ;
  4. foreach

    (

    $sub

    as

    $subcomm

    )

    {
  5. echo

    '<tr>'

    ;

  6. $mydb

    ->

    setQuery

    (

    "SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}

    '"

    )

    ;
  7. $propic

    =

    $mydb

    ->

    loadResultList

    (

    )

    ;
  8. if

    (

    $mydb

    ->

    affected_rows

    (

    )

    ==

    0

    )

    {
  9. echo

    '<td><img src="./uploads/p.jpg" class="img-object" width="30px" height=40px" /></td>'

    ;

  10. }
  11. foreach

    (

    $propic

    as

    $obj

    )

    {
  12. echo

    '<td >'

    ;
  13. echo

    '<img src="./uploads/'

    .

    $obj

    ->

    filename

    .

    '" class="img-object" width="30px" height="40px" />'

    ;
  14. echo

    '</td>'

    ;
  15. }
  16. echo

    '<td><a href="home.php?id='

    .

    $_SESSION

    [

    'member_id'

    ]

    .

    '">'

    .

    $comm

    ->

    author

    .

    '</a>'

    ;
  17. echo

    '<br/><p>'

    .

    $subcomm

    ->

    subcontent

    .

    '</p></td>'

    ;
  18. echo

    '</tr>'

    ;

And after executing this code, you can test it in your browser and it will look like as shown below.

sub2.png

Here’s now 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. <title>Philsocial</title>

  13. <!-- Bootstrap core CSS -->
  14. <link href="css/bootstrap.css" rel="stylesheet">

  15. <!-- Custom styles for this template -->
  16. <link href="jumbotron.css" rel="stylesheet">

  17. <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  18. <!--[if lt IE 9]>
  19. <script src="../../assets/js/html5shiv.js"></script>
  20. <script src="../../assets/js/respond.min.js"></script>
  21. <![endif]-->
  22. <?php
  23. //login confirmation
  24. confirm_logged_in(

    )

    ;
  25. ?>
  26. </head>

  27. <body>

  28. <div class="navbar navbar-inverse navbar-fixed-top">
  29. <div class="container">
  30. <div class="navbar-header">
  31. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
  32. <span class="icon-bar"></span>
  33. <span class="icon-bar"></span>
  34. <span class="icon-bar"></span>
  35. </button>
  36. <a class="navbar-brand" href="home.php"><B>Philsocial</B></a>
  37. </div>
  38. <form class="navbar-form navbar-left">
  39. <div class="form-group">
  40. <div class="rows">
  41. <input type="text" placeholder="Email" class="form-control" size="40">
  42. </div>
  43. </div>
  44. </form>
  45. <div class="navbar-collapse collapse">

  46. <form class="navbar-form navbar-right">
  47. <ul class="nav navbar-nav">
  48. <li class="active"><a href="home.php">Home</a></li>
  49. <li class="dropdown">

  50. <a href="#" class="dropdown-toggle" data-toggle="dropdown">
  51. <?php
  52. //retrieve session variable
  53. echo

    $_SESSION

    [

    'fName'

    ]

    ;

    ?>
  54. <b class="caret"></b>
  55. </a>

  56. <ul class="dropdown-menu">
  57. <li><a href="#">My Profile</a></li>
  58. <li><a href="#">Edit profile</a></li>
  59. <li><a href="#">Edit profile Picture</a></li>
  60. <li><a href="#">Customize profile</a></li>
  61. <li><a href="#">Edit Work and Education</a></li>

  62. </ul>
  63. </li>
  64. <li class="dropdown">

  65. <a href="#" class="dropdown-toggle" data-toggle="dropdown">Account<b class="caret"></b></a>

  66. <ul class="dropdown-menu">
  67. <li><a href="#">Account Settings</a></li>
  68. <li><a href="#">Privacy Settings</a></li>
  69. <li><a href="#">Manage Social Accounts</a></li>
  70. <li><a href="#">Manage Credits</a></li>
  71. <li><a href="logout.php">Logout</a></li>

  72. </ul>
  73. </li>
  74. </ul>
  75. </form>
  76. </div><!--/.navbar-collapse -->
  77. </div>
  78. </div>
  79. <div class="container">
  80. <div class="well">

  81. <div class="row">
  82. <div class="col-xs-6 col-md-2">
  83. <a data-target="#myModal" data-toggle="modal" href="" title=
  84. "Click here to Change Image.">
  85. <?php

  86. $mydb

    ->

    setQuery

    (

    "SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}

    '"

    )

    ;
  87. $cur

    =

    $mydb

    ->

    loadResultList

    (

    )

    ;
  88. if

    (

    $mydb

    ->

    affected_rows

    (

    )

    ==

    0

    )

    {
  89. echo

    '<img src="./uploads/p.jpg" class="img-thumbnail" width="200px" height="100px" />'

    ;

  90. }
  91. foreach

    (

    $cur

    as

    $object

    )

    {

  92. echo

    '<img src="./uploads/'

    .

    $object

    ->

    filename

    .

    '" class="img-thumbnail" width="200px" height="100px" />'

    ;

  93. }
  94. ?>
  95. </a>

  96. <!-- Modal -->
  97. <div class="modal fade" id="myModal" tabindex="-1">
  98. <div class="modal-dialog">
  99. <div class="modal-content">
  100. <div class="modal-header">
  101. <button class="close" data-dismiss="modal" type=
  102. "button">Ă—</button>

  103. <h4 class="modal-title" id="myModalLabel">Choose Your best
  104. picture for your Profile.</h4>
  105. </div>

  106. <form action="save_photo.php" enctype="multipart/form-data" method=
  107. "post">
  108. <div class="modal-body">
  109. <div class="form-group">
  110. <div class="rows">
  111. <div class="col-md-12">
  112. <div class="rows">
  113. <div class="col-md-8">
  114. <input name="MAX_FILE_SIZE" type=
  115. "hidden" value="1000000"> <input id=
  116. "upload_file" name="upload_file" type=
  117. "file">
  118. </div>

  119. <div class="col-md-4"></div>
  120. </div>
  121. </div>
  122. </div>
  123. </div>
  124. </div>

  125. <div class="modal-footer">
  126. <button class="btn btn-default" data-dismiss="modal" type=
  127. "button">Close</button> <button class="btn btn-primary"
  128. name="savephoto" type="submit">Save Photo</button>
  129. </div>
  130. </form>
  131. </div><!-- /.modal-content -->
  132. </div><!-- /.modal-dialog -->
  133. </div><!-- /.modal -->
  134. </div>

  135. <div class="col-xs-12 col-sm-6 col-md-8">
  136. <div class="page-header">
  137. <h3><?php

    echo

    $_SESSION

    [

    'fName'

    ]

    .

    ' '

    .

    $_SESSION

    [

    'lName'

    ]

    ;

    ?>

    </h3>
  138. </div>

  139. <ul class="nav nav-tabs">
  140. <li class="active">
  141. <a href="#">Wall</a>
  142. </li>

  143. <li>
  144. <a href="info.php">Info</a>
  145. </li>

  146. <li>
  147. <a href="message.php">Messages</a>
  148. </li>
  149. </ul>
  150. <div class="well">
  151. <div class="panel-group" id="accordion">
  152. <div class="panel panel-primary">
  153. <div class="panel-heading">
  154. <h5 class="panel-title">
  155. <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" title="What's on your mind?">
  156. Update Status
  157. </a>
  158. </h5>
  159. </div>

  160. <form action="save_post.php" method="POST">
  161. <div id="collapseOne" class="panel-collapse collapse">
  162. <div class="panel-body">
  163. <input type="hidden" name="comment_id" value="<?php

    echo

    $_SESSION

    [

    'member_id'

    ]

    ;

    ?>

    ">
  164. <input type="hidden" name="author" value="<?php

    echo

    $_SESSION

    [

    'fName'

    ]

    .

    ' '

    .

    $_SESSION

    [

    'lName'

    ]

    ;

    ?>

    ">
  165. <input type="hidden" name="to" value="<?php

    echo

    $_SESSION

    [

    'member_id'

    ]

    ;

    ?>

    ">
  166. <textarea class="form-control" name="content" placeholder="What's on your mind?"></textarea>

  167. </div>
  168. <div class="panel-footer" align="right">
  169. <button class="btn btn-primary btn-sm" type="submit" name="share">Share</button>
  170. </div>
  171. </div>
  172. </form>
  173. </div>
  174. </div>
  175. <?php
  176. global

    $mydb

    ;
  177. $mydb

    ->

    setQuery

    (

    "SELECT * from comments where comment_id="

    .

    $_SESSION

    [

    'member_id'

    ]

    .

    " ORDER BY created DESC"

    )

    ;
  178. $cur

    =

    $mydb

    ->

    loadResultList

    (

    )

    ;

  179. echo

    '<div class="table table-responsive" border="0">'

    ;
  180. echo

    '<table>'

    ;
  181. echo

    '<tr>'

    ;

  182. foreach

    (

    $cur

    as

    $comm

    )

    {
  183. $mydb

    ->

    setQuery

    (

    "SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}

    '"

    )

    ;
  184. $propic

    =

    $mydb

    ->

    loadResultList

    (

    )

    ;
  185. if

    (

    $mydb

    ->

    affected_rows

    (

    )

    ==

    0

    )

    {
  186. echo

    '<td rowspan="2"><img src="./uploads/p.jpg" class="img-object" width="50px" height=60px" /></td>'

    ;

  187. }
  188. foreach

    (

    $propic

    as

    $obj

    )

    {
  189. echo

    '<td rowspan="2">'

    ;
  190. echo

    '<img src="./uploads/'

    .

    $obj

    ->

    filename

    .

    '" class="img-object" width="50px" height="60px" />'

    ;
  191. echo

    '</td>'

    ;
  192. }
  193. echo

    '</tr>'

    ;

  194. echo

    '<tr>'

    ;

  195. echo

    '<td><strong><a href="home.php?id='

    .

    $_SESSION

    [

    'member_id'

    ]

    .

    '">'

    .

    $comm

    ->

    author

    .

    '</a></strong>'

    ;

  196. echo

    '<br/><p>'

    .

    $comm

    ->

    content

    .

    '</p><br/>'

    ;

  197. echo

    '<table border="0">'

    ;
  198. /* this area is for listing of sub-comment*/
  199. $mydb

    ->

    setQuery

    (

    "SELECT * FROM `subcomment` WHERE `comment_id` = "

    .

    $comm

    ->

    id

    )

    ;
  200. $sub

    =

    $mydb

    ->

    loadResultList

    (

    )

    ;
  201. foreach

    (

    $sub

    as

    $subcomm

    )

    {
  202. echo

    '<tr>'

    ;

  203. $mydb

    ->

    setQuery

    (

    "SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}

    '"

    )

    ;
  204. $propic

    =

    $mydb

    ->

    loadResultList

    (

    )

    ;
  205. if

    (

    $mydb

    ->

    affected_rows

    (

    )

    ==

    0

    )

    {
  206. echo

    '<td><img src="./uploads/p.jpg" class="img-object" width="30px" height=40px" /></td>'

    ;

  207. }
  208. foreach

    (

    $propic

    as

    $obj

    )

    {
  209. echo

    '<td >'

    ;
  210. echo

    '<img src="./uploads/'

    .

    $obj

    ->

    filename

    .

    '" class="img-object" width="30px" height="40px" />'

    ;
  211. echo

    '</td>'

    ;
  212. }
  213. echo

    '<td><a href="home.php?id='

    .

    $_SESSION

    [

    'member_id'

    ]

    .

    '">'

    .

    $comm

    ->

    author

    .

    '</a>'

    ;
  214. echo

    '<br/><p>'

    .

    $subcomm

    ->

    subcontent

    .

    '</p></td>'

    ;
  215. echo

    '</tr>'

    ;



  216. }

  217. //This area is for creating a new comment

  218. echo

    '<tr>'

    ;
  219. echo

    '<form action="save_subcomm.php" method="post">'

    ;
  220. echo

    '<input name="commentid" type="hidden" value="'

    .

    $comm

    ->

    id

    .

    '">'

    ;
  221. echo

    '<input name="subauthor" type="hidden" value="'

    .

    $_SESSION

    [

    'fName'

    ]

    .

    ' '

    .

    $_SESSION

    [

    'lName'

    ]

    .

    '">'

    ;

  222. $mydb

    ->

    setQuery

    (

    "SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}

    '"

    )

    ;
  223. $propic

    =

    $mydb

    ->

    loadResultList

    (

    )

    ;
  224. if

    (

    $mydb

    ->

    affected_rows

    (

    )

    ==

    0

    )

    {
  225. echo

    '<td><img src="./uploads/p.jpg" class="img-object" width="30px" height=30px" /></td>'

    ;

  226. }
  227. foreach

    (

    $propic

    as

    $obj

    )

    {
  228. echo

    '<td >'

    ;
  229. echo

    '<img src="./uploads/'

    .

    $obj

    ->

    filename

    .

    '" class="img-object" width="30px" height="30px" />'

    ;
  230. echo

    '</td>'

    ;
  231. }

  232. echo

    '<td><input name="subcontent" type="text" style="width: 370px;" class="form-control" placeholder="Write a comment...">'

    ;
  233. echo

    '</form>'

    ;
  234. echo

    '</tr>'

    ;
  235. echo

    '</table>'

    ;
  236. //echo '</div>';
  237. /*
  238. End of New sub comment.
  239. */
  240. echo

    '</div>'

    ;
  241. echo

    '</div>'

    ;
  242. echo

    '</tr>'

    ;


  243. }
  244. echo

    '</table>'

    ;
  245. ?>

  246. </div>

  247. </div>
  248. </div>
  249. </div>
  250. </div>



  251. </body>
  252. </html>

  253. <hr>

  254. </div> <!-- /container -->
  255. <footer>
  256. <p align="center">&copy; Philsocial 2013</p>
  257. </footer>
  258. <!-- Bootstrap core JavaScript
  259. ================================================== -->
  260. <!-- Placed at the end of the document so the pages load faster -->
  261. <script src="js/tooltip.js"></script>
  262. <script src="assets/js/jquery.js"></script>
  263. <script src="js/bootstrap.min.js"></script>
  264. <script src="js/popover.js"></script>


  265. </body>
  266. </html>

 

452,292

324,360

324,368

Top