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. In this third part, we will be making a thread page in order to display the full thread and offer the chance for replies. 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 and second parts of this tutorial series which can both be found on my profile tracking page. Main Page Editing: Because I was not originally going to add in replies to deeply to this tutorial series, it did not require a separate thread page; but now it does. So, going back to our main page where we list the threads, we want to encase the title of the each thread with a link to a new page along with the get statement of the thread ID contained in the unique 'id' field of the tables 'thread' within the database. So change;
  1. echo '<tr><td>'.$row["title"].'</td><td>'.$content.'...</td></tr>';
to;
  1. echo '<tr><td><a href="threadPage.php?tid='.$row["id"].'">'.$row["title"].'</td><td>'.$content.'...</td></tr>';
which will now add a link to each threads title going to a new 'threadPage.php' page along with it's specific ID to get the thread information (make this page now, we will edit it in the next section of this tutorial:). Getting Thread Information: So now we have a thread page, but it is blank - no matter what we put for the Thread ID (tid)... First we want to continue the session of PHP for the logged in users, and connect to our database;
  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysql_error());
  3. ?>
Next we want to set the variables which will become the information for the thread, we set these to nothing for now to avoid getting errors if the information does not exist (tid isn't set), we also get the tid, and check if it's not blank. If it is blank we output 'fail tid.';
  1. $title = '';
  2. $content = '';
  3. $author = '';
  4. if (isSet($_GET['tid']) && $_GET['tid'] != '') {
  5. }else
  6. echo 'fail tid.';
Now that we can confirm the tid is not blank and is set, we want to get all the rows from our 'threads' table that use our id (it should only be one since the id field is AI and unique). Once we have sent the query we check to see if the row exists, if it doesn't we output 'fake tid.';
  1. $id = $_GET['tid'];
  2. $query = mysqli_query($con, "SELECT * FROM `threads` WHERE `id`='$id'");
  3. if (mysqli_num_rows($query) > 0) {
  4.  
  5. }else
  6. echo 'fake tid.';
Now we want to set some variables of the information given from the php query;
  1. $info = mysqli_fetch_array($query);
  2. $title = $info['title'];
  3. $content = $info['content'];
  4. $author = 'By '.$info['author'];
Finally we want to output the information in the given places;
  1. <html>
  2. <head></head>
  3. <body>
  4. <h1><?php echo $title; ?></h1>
  5. <p><?php echo $content; ?></p>
  6. <br/>
  7. <p>By <?php echo $author; ?></p>
  8. </body>
  9. </html>
Full Source: Here is the full source just in case you got lost, but I would highly recommend making sure you know what each part does by referencing to the above descriptions.
  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysql_error());
  3. $title = '';
  4. $content = '';
  5. $author = '';
  6. if (isSet($_GET['tid']) && $_GET['tid'] != '') {
  7. $id = $_GET['tid'];
  8. $query = mysqli_query($con, "SELECT * FROM `threads` WHERE `id`='$id'");
  9. if (mysqli_num_rows($query) > 0) {
  10. $info = mysqli_fetch_array($query);
  11. $title = $info['title'];
  12. $content = $info['content'];
  13. $author = 'By '.$info['author'];
  14. }else
  15. echo 'fake tid.';
  16. }else
  17. echo 'fail tid.';
  18. ?>
  19. <html>
  20. <head></head>
  21. <body>
  22. <h1><?php echo $title; ?></h1>
  23. <p><?php echo $content; ?></p>
  24. <br/>
  25. <p>By <?php echo $author; ?></p>
  26. </body>
  27. </html>

Add new comment