PHP/MySQLi Creating a Forum - Part 4 - Adding & Listing Replies

PHP/MySQLi Creating a Forum - Part 3 - Thread Page Introduction: This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. This is the fourth and (for now) the final part which is adding reply functionality. Pre-creation: First you will need a host for your PHP, either a web host or localhost is fine but you will need PHP and MySQL(i) capabilities. Also, this will not be covering creating users, or styling the pages. For the purpose of using the logged in users username, we will be using $_SESSION['username']; from my login script, you can find that tutorial on my profile page. Obviously you will also need to go through the first, second and third parts of this tutorial series which can both be found on my profile tracking page. Thread Page Editing: Last tutorial we created a separate thread page in order for us to offer the chance of adding replies. So, we are going to add a form to add a reply at the bottom of the page;
  1. <br/>
  2. <h1>Leave a Reply:</h1>
  3. <form action=<?php echo 'threadPage.php?tid='.$_GET['tid'];?> method='POST'>
  4.         <table>
  5.                 <tbody>
  6.                         <tr>
  7.                                 <td>Name: </td><td><?php echo $_SESSION['username']; ?></td>
  8.                         </tr>
  9.                         <tr>
  10.                                 <td>Message: </td><td><input type='text' name='cont' /></td>
  11.                         </tr>
  12.                         <tr>
  13.                                 <td></td><td><input type='submit' value='Post Reply' name='replySent' /></td>
  14.                         </tr>
  15.                 </tbody>
  16.         </table>
  17. </form>
As you can (and should be able to) see, we are setting the action of the form to the same page (threadPage.php) along with the thread ID of the current tid, this keeps the content the same whilst and after adding the reply. Table: I have decided to do the table a little bit different to what I first said in the first part of this tutorial series. We are going to have the following structure: id - int - 5 Length - AI - Primary Key threadID - VARCHAR - 255 Length content - VARCHAR - 255 Length author - VARCHAR - 255 Length And put the table name to 'replies' (you can delete the other one if you made one from the first tutorial, sorry about that!) PHP Programming: Next, we need to process the reply form using some PHP. Again, we are going to check and confirm the validity of the passing variables, we are then going to insert the reply as a new row if each variable is ok. Finally we give the output;
  1. if (isSet($_POST['replySent']) && isSet($_POST['cont']) && $_POST['cont'] != '' && isSet($_GET['tid']) && $_GET['tid'] != '') {
  2.         $cont = $_POST['cont'];
  3.         $thread = $_GET['tid'];
  4.         $user = $_SESSION['username'];
  5.         $q = mysqli_query($con, "INSERT INTO `replies` VALUES ('', '$thread', '$cont', '$user')");
  6.         if ($q) {
  7.                 echo 'Reply submitted.';
  8.         }else
  9.                 echo 'Failed inserting reply.';
  10. }
Listing Replies: Finally, we need to list all the replies relevant to each thread. On the threadPage we are going to get the thread id (tid) from the get url of the page, we are then going to run a query to get all the rows within the replies table which have threadID as the tid, and output each one separately;
  1.                         $qu = mysqli_query($con, "SELECT * FROM `replies` WHERE `threadID`='$id'");
  2.                         if (mysqli_num_rows($qu) > 0) {
  3.                                 $replies = '<table><tbody>';
  4.                                 while ($row = mysqli_fetch_array($qu)) {
  5.                                         $replies .= '<tr><td>'.$row["content"].'</td><td>'.$row["author"].'</td></tr>';
  6.                                 }
  7.                                 $replies .= '</tbody></table>';
  8.                         }
We want to put the above code just underneath where we set $info, this is because that means we have validated our tid and it is corrent. We are putting all of the replies in to one variable (named replies) as a table of HTML so we can simply output it later by writing the variable. Because of this, we will get an error if there are no replies because $replies won't exist, to fix this just create $replies as a blank variable outside of any if statements;
  1. $replies = '';
Lastly we want to output the variable $replies through PHP in to our HTML body at the bottom of our thread page;
  1. <?php echo $replies; ?>
Finished! Congrats on finishing the tutorial series, feel free to send me a message for any advice, notes, queries or feedback you have, thank you! Full Source: Here is the full source for the threadPage.php;
  1. <?php
  2.         session_start();
  3.         $con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysql_error());
  4.         if (isSet($_POST['replySent']) && isSet($_POST['cont']) && $_POST['cont'] != '' && isSet($_GET['tid']) && $_GET['tid'] != '') {
  5.                 $cont = $_POST['cont'];
  6.                 $thread = $_GET['tid'];
  7.                 $user = $_SESSION['username'];
  8.                 $q = mysqli_query($con, "INSERT INTO `replies` VALUES ('', '$thread', '$cont', '$user')");
  9.                 if ($q) {
  10.                         echo 'Reply submitted.';
  11.                 }else
  12.                         echo 'Failed inserting reply.';
  13.         }
  14.         $replies = '';
  15.         if (isSet($_GET['tid']) && $_GET['tid'] != '') {
  16.                 $id = $_GET['tid'];
  17.                 $query = mysqli_query($con, "SELECT * FROM `threads` WHERE `id`='$id'");
  18.                 if (mysqli_num_rows($query) > 0) {
  19.                         $info = mysqli_fetch_array($query);
  20.                         $qu = mysqli_query($con, "SELECT * FROM `replies` WHERE `threadID`='$id'");
  21.                         if (mysqli_num_rows($qu) > 0) {
  22.                                 $replies = '<table><tbody>';
  23.                                 while ($row = mysqli_fetch_array($qu)) {
  24.                                         $replies .= '<tr><td>'.$row["content"].'</td><td>'.$row["author"].'</td></tr>';
  25.                                 }
  26.                                 $replies .= '</tbody></table>';
  27.                         }
  28.                         $title = $info['title'];
  29.                         $content = $info['content'];
  30.                         $author = $info['author'];
  31.                 }else
  32.                         echo 'fake tid.';
  33.         }else
  34.                 echo 'fail tid.';
  35. ?>
  36. <html>
  37.         <head></head>
  38.         <body>
  39.                 <h1><?php echo $title; ?></h1>
  40.                 <p><?php echo $content; ?></p>
  41.                 <br/>
  42.                 <p>By <?php echo $author; ?></p>
  43.                 <br/>
  44.                 <h1>Leave a Reply:</h1>
  45.                 <form action=<?php echo 'threadPage.php?tid='.$_GET['tid'];?> method='POST'>
  46.                         <table>
  47.                                 <tbody>
  48.                                         <tr>
  49.                                                 <td>Name: </td><td><?php echo $_SESSION['username']; ?></td>
  50.                                         </tr>
  51.                                         <tr>
  52.                                                 <td>Message: </td><td><input type='text' name='cont' /></td>
  53.                                         </tr>
  54.                                         <tr>
  55.                                                 <td></td><td><input type='submit' value='Post Reply' name='replySent' /></td>
  56.                                         </tr>
  57.                                 </tbody>
  58.                         </table>
  59.                 </form>
  60.                 <br/>
  61.                 <?php echo $replies; ?>
  62.         </body>
  63. </html>

Comments

Add new comment