Make PHP Sites

Last updated: 2015-09-13

Viewing a forum thread

In this section, we'll go over how to allow a registered user to view a thread in a forum.

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 post to this forum.

Contents

The database

In the previous lessons, User registration and User log-in and log-out, we created and worked with a database and table. We'll be using that same setup with modifications. We're adding the fields "fname," "lname" and "phone."

Database name: social

We'll use this database for more social apps

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 thread

First, let's render the HTML we'll use to display a thread.

					<html>
						<head>
							<title>THREAD TITLE</title>
						</head>
						
						<body>
						
							<h2>THREAD TITLE</h2>
							
							By THREAD AUTHOR USERNAME | DATE POSTED | Comments: NUMBER OF COMMENTS
							
							<p>
								THREAD CONTENT
							</p>
						
						</body>
					</html>
				

Right now, our page looks like this:

Picture of a template thread page

This general template will fit the information we want to display to a user on each individual thread. Let's query the database for the information about the thread. This can only be done if the ID of a thread is in the URL as a $_GET variable. For this lesson, only logged-in users can view forum threads. If they are, query the database for the thread.

					<?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 * FROM forum_threads WHERE threadID=" . $threadID . " AND isDeleted!=1";
							$query = $mysli->query($sql);
							
							// Get the whole array of data
							$thread = $query->fetch_assoc();
						}
					?>
					
					<html>
						[...]
					</html>
				

In the above code, we are verifying the user is real and logged in, making sure the user is looking for a specific thread, connecting to the database and getting the information about the thread.

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, $thread would look similar to this:

					Array(
						[threadID] => 1,
						[boardID] => 3,
						[userID] => 17,
						[title] => 'Title of this thread',
						[content] => 'Content within this thread.',
						[comments] => 0,
						[created] => 1586082718,
						[isDeleted] => ,
						[timestamp] => 2020-04-05T06:31:58-04:00						
						)
				

Using that information, we can fill in our thread page. Let's adjust the HTML.

					<?php
						[...]
					?>
					
					<html>
						<head>
							<title>
								<?php echo $thread['title']; ?>
							</title>
						</head>
						
						<body>
						
							<h2>
								<?php echo $thread['title']; ?>
							</h2>
							
							By <?php echo $thread['userID']; ?> | <?php echo date('M d, Y - h:ia', $thread['created']); ?> | Comments: <?php echo $thread['comments']; ?>
							
							<p>
								<?php echo $thread['content']; ?>
							</p>
						
						</body>
					</html>
				

Notice the date() piece. It's configuring the time the thread was created into a common format using the integer stored in $thread['created'].

If everything is working and a thread by the ID number in the $_GET['threadID'] variable exists, your page should look similar to this:

Notice how the author of this thread is identified under the title by the userID value. In the User registration section, the user did not create a username. We modified that in Forum section about the database by explaining the addition of a username column to the users table. We'll continue assuming you have added that column. Otherwise, you could use any of the other information abou the user that we're about to obtain.

To use something other than the user's identification number, we need to modify the query that got the thread.

					// 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);
				

By making that change, our query now collects information from the forum_threads table and the users table that match the author's identification number and the thread's identification number. Our $thread array now looks something like this:

					Array(
						[threadID] => 1,
						[boardID] => 3,
						[userID] => 17,
						[title] => 'Title of this thread',
						[content] => 'Content within this thread.',
						[comments] => 0,
						[created] => 1586082718,
						[isDeleted] => ,
						[timestamp] => 2020-04-05T06:31:58-04:00,
						['username'] => 'some_username'
						
						)
				

Adjust the HTML to include the username instead of the userID. Your code and web page should looks as follows:

					By <?php echo $thread['username']; ?>
				

Notice where the $thread['content']; ?> variable is written. What if the user entered it as multiple lines instead of one line of text? We'll use the function n2lbr() to display those new lines.

					<?php
						[...]
					?>
					
					<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>
				

For our demonstration, we submitted the content on different lines. So, our page now looks like this:

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>
				
Next, we'll work on making comments on a post and displaying those comments under the post.

Contact us

Questions or problems? Want to contribute to this tutorial or others?

Contact us