PHP/MySQLi Creating a Forum - Part 2 - Listing Threads

PHP/MySQLi Creating a Forum - Part 1 - Database and Creating Threads Introduction: This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. As this is the second part, we will be making a list of the created threads. 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 part of this tutorial series which can be found on my profile tracking page. Listing Threads: For this, I am going to simply add a list to the bottom of the page we created in the first tutorial - this just keeps it simple for the tutorials sake. However, you can put it in to a new page if you want; ensure you create a new connection and continue the session. So first lets create a table in order to make the layout grid-like and ensure it looks neat (I would recommend using CSS instead, but I am not using CSS in this series).
  1. <h1>Threads:</h1>
  2. <table>
  3. <tbody>
  4. </tbody>
  5. </table>
Now we will use PHP to run through each thread within our 'threads' table to output each threads title in the first row of each table;
  1. <?php
  2. $qu = mysqli_query($con, "SELECT * FROM `threads`");
  3. if (mysqli_num_rows($qu) > 0) {
  4. while ($row = mysqli_fetch_array($qu)) {
  5. echo '<tr><td>'.$row["title"].'</td></tr>';
  6. }
  7. }else{
  8. echo '<tr><td>No threads found :(</tr></td>';
  9. }
  10. ?>
Small Description: This step is not needed but we are going to add a short description of 100 characters to a second column of each threads row; First we need to get the description;
  1. $content = $row['content'];
Then if the string length of the content is greater than 100 we change the value over to a new variable named a, clear content, then transfer the first 100 characters from a back over to content...
  1. if (strlen($content) > 100) {
  2. $a = $content;
  3. $content = '';
  4. for($i=0;$i<100;$i++) {
  5. $content .= $a[$i];
  6. }
  7. }
We also need to fix our output...
  1. echo '<tr><td>'.$row["title"].'</td><td>'.$content.'...</td></tr>';
Full Source: Here is the source for this tutorial;
  1. <h1>Threads:</h1>
  2. <table>
  3. <tbody>
  4. <?php
  5. $qu = mysqli_query($con, "SELECT * FROM `threads`");
  6. if (mysqli_num_rows($qu) > 0) {
  7. while ($row = mysqli_fetch_array($qu)) {
  8. $content = $row['content'];
  9. if (strlen($content) > 100) {
  10. $a = $content;
  11. $content = '';
  12. for($i=0;$i<100;$i++) {
  13. $content .= $a[$i];
  14. }
  15. }
  16. echo '<tr><td>'.$row["title"].'</td><td>'.$content.'...</td></tr>';
  17. }
  18. }else{
  19. echo '<tr><td>No threads found :(</tr></td>';
  20. }
  21. ?>
  22. </tbody>
  23. </table>

Comments

Submitted byryr11 (not verified)on Wed, 02/12/2014 - 06:31

In the final code download, you have this line $q = mysqli_query($con, "INSERT INTO `threads` VALUES ('', '$title', '$description', '$user', '0', '0', '$tags')") or die(mysql_error());, but in the first thread you only have us make the id, title, content, and author tags what are the last 3?
Submitted bybisinotto (not verified)on Sat, 04/05/2014 - 16:09

Thanks you for this tutorial is very helpful for my work every days

Add new comment