Viewing a forum board
In this section, we'll go over how to allow a registered user to view threads on a forum board.
The code for this version will be written in a procedural method.
Requirements
Refer to the Get Started section to know what you need to use this tutorial. You must also have an understanding of HTML and how a web page is constructed.
Prerequisites
To use this tutorial, we recommend that you use our other lessons, User registration, User log-in and log-out, Setting restrictions with PHP, Posting a forum thread and PHP forum - The database to be able to duplicate what we have.
The database described in those tutorials is necessary if you plan to follow this tutorial line-by-line. Only logged-in users will be able to view threads on this forum.
Contents
The database
For all information about the database we'll be using for this tutorial refer to PHP Forum - The database.
Database name: social
We'll use this database for more social apps
Table: forum_boards
Column | Type | Description |
---|---|---|
boardID | INT (11) | This will be the primary key. It is a unique ID number assigned to each individual board. It should be set to increment automatically. |
board | VARCHAR (250) | This holds the name of the board. |
isDeleted | Bool | A TRUE/FALSE value that will be used to control whether or not the board can be found. |
timestamp | Timestamp | A time stamp, provided by the database, about time the board was last updated. |
The table forum_boards
will hold the information about each table you want users to use.
Table: forum_threads
Column | Type | Description |
---|---|---|
threadID | INT (11) | This will be the primary key. It is a unique ID number assigned to each individual thread. It should be set to increment automatically. |
boardID | INT (11) | The identification number of the board each thread belongs to. |
userID | INT (11) | The identification number of the user who created the thread. |
title | TEXT | The string of text the user enters as the title of the thread. You can set it to a limited number of letters. |
content | LONGTEXT | The string of text the user entered as the body of their thread. |
comments | INT (11) | The number of comments a thread has received. |
created | INT (11) | The numberic value of the time a thread was created |
isDeleted | Bool | A TRUE/FALSE value that will be used to tell if a user has canceld there account or has been blocked. |
timestamp | Timestamp | A time stamp, provided by the database, about time the thread was last updated. |
Displaying the threads of a single forum board
In this tutorial, we're assuming you've created the same database as instructed. It involves multiple boards for this forum.
First, let's render the HTML we'll use to display threads.
<html> <head> <title>BOARD TITLE</title> </head> <body> <h2>BOARD TITLE</h2> <p>Other boards</p> <ul> <li><a href="">BOARD NAME</a></li> <li><a href="">BOARD NAME</a></li> <li><a href="">BOARD NAME</a></li> </ul> <ul> <li> <h3 class="thread-title">THREAD TITLE</h3> <span class="thread-author">THREAD AUTOR</> <span class="thread-date">THREAD DATE</span> </li> </ul> </body> </html>
Why is the format a <ul>
? We chose <ul>
instead of <table>
so that it can be more easily manipulated by CSS later on if we wanted to.
Right now, our page looks like this:

This general template will fit the information we want to display to a user for several threads on each board. Notice the list of BOARD NAME
about the threads. Since this is a forum with a multiple boards, we might as well list them on each board. Let's query the database for a list of the boards so we make make the links.
<?php session_start(); if(!isset($_SESSION['userID']) || !isset($_SESSION['email'])){ header('Location: /login.php'); } // Connect to the database // What's the address of your database? $database_host = 'localhost'; // What's the username to connect? $database_user = 'root'; // What's the password for that user? $database_password = 'password'; // What's the name of the database? $database = 'social'; // User the MySQLi class to connect to database $mysqli = new mysqli($database_host, $database_user, $database_password, $database); // Query for the available forum boards $sql = "SELECT * FROM forum_boards WHERE isDeleted!=1"; $query = $mysqli->query($sql); // User fetch_assoc() to get all the rows $boards = array(); while($rows = $query->fetch_assoc()){ $boards[] = $rows; } ?> <html> [...] </html>
In the above code, we are verifying the user is real and logged in. Then, we're getting a list of the available boards.
Notice the fetch_assoc()
function attached to the query. That function is built into the mysqli
class which interacts with the database. Using fetch_assoc()
, we can pull the information out of the database as an array. If done correctly, $boards
would look similar to this:
Array( [0] => Array( [boardID] => 1, [board] => 'Console games', [isDeleted] => , [timestamp] => 2021-01-19T13:56:39-05:00), [1] => Array( [boardID] => 2, [board] => 'Handheld games', [isDeleted] => , [timestamp] => 2021-01-19T13:56:39-05:00), [2] => Array( [boardID] => 3, [board] => 'Computer games', [isDeleted] => , [timestamp] => 2021-01-19T13:56:39-05:00), [3] => Array( [boardID] => 4, [board] => 'Mobile games', [isDeleted] => , [timestamp] => 2021-01-19T13:56:39-05:00) )
Using that information, we can create the list of board links. Let's adjust the HTML.
<?php [...] ?> <html> <head> <title>BOARD TITLE</title> </head> <body> <h2>BOARD TITLE</h2> <p>Other boards</p> <ul> <?php foreach($boards as $board): ?> <li><a href=""><?php echo $board['board']; ?></a></li> <?php endforeach; ?> </ul> <ul> <li> <h3 class="thread-title">THREAD TITLE</h3> <span class="thread-author">THREAD AUTOR</> <span class="thread-date">THREAD DATE</span> </li> </ul> </body> </html>
Use the foreach
loop to add a link to each board on the page.
If everything is working correctly, your page should look like this:

Next, let's query the database for a number of threads from the board we're visiting. We need to include in our query the identification number of the board. Our tutorial assumes there's a variable in the URL such as boardID=1
for that purpose.
Modify your PHP to include this script:
All the code
<?php // Is the user logged in? session_start(); if(!isset($_SESSION['userID']) || !isset($_SESSION['email'])){ header('Location: /login.php'); } // Make sure a threadID is present if(!isset($_GET['threadID']) || !is_numeric($_GET['threadID'])){ // If no ID was mentioned, or it isn't a number, readirect the user header('Location: /forum'); } else { // Get the ID number $threadID = $_GET['threadID']; // Connect to the database // What's the address of your database? $database_host = 'localhost'; // What's the username to connect? $database_user = 'root'; // What's the password for that user? $database_password = 'password'; // What's the name of the database? $database = 'social'; // User the MySQLi class to connect to database $mysqli = new mysqli($database_host, $database_user, $database_password, $database); // Get the single thread $sql = "SELECT forum_threads.*, users.username FROM forum_threads JOIN users ON users.userID=forum_threads.userID WHERE forum_threads.threadID=" . $threadID . " AND forum_threads.isDeleted!=1"; $query = $mysqli->query($sql); // Get the whole array of data $thread = $query->fetch_assoc(); } ?> <html> <head> <title> <?php echo $thread['title']; ?> </title> </head> <body> <h2> <?php echo $thread['title']; ?> </h2> By <?php echo $thread['username']; ?> | <?php echo date('M d, Y - h:ia', $thread['created']); ?> | Comments: <?php echo $thread['comments']; ?> <p> <?php echo nl2br($thread['content']); ?> </p> </body> </html>
Contact us
Questions or problems? Want to contribute to this tutorial or others?