PHP/MySQLi Creating a Forum - Part 10 - Latest Threads
!!! Section 1 !!!
Introduction:
This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. In this eleventh part, We will begin creating a user page which will contains all the threads posted by that user, personal messaging and possibly some more extras as wel go along.
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, third, fourth, fifth, sixth, seventh, eight, ninth and tenth parts of this tutorial series which can all be found on my profile tracking page.
This tutorial:
Since this is the first tutorial of a user page series within my forum series, we are going to be setting up the messaging database, as well as the user account page.
User Page:
We will create a page named 'userPage.php' which will take the GET argument of the username and wil display the information of the given user. Just put the basics in for now...
<?php
?>
<html>
<head></head>
<body></body>
</html>
Messaging Database:
For this messaging database we will name the new table 'messages' and it will need three custom values and the id, so four columns in total, like so...
id - INT - 5 Length - Primary Key - A_I (AI, Auto Increment)
from - VARCHAR - 255 Length
message- VARCHAR - 255 Length
to - VARCHAR - 255 Length
!!! Section 2 !!!
Introduction:
This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. In this twelfth part, We will be continuing our user page, more precisely, we will be displaying all the threads that user has posted.
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, third, fourth, fifth, sixth, seventh, eight, ninth, tenth and twelfth parts of this tutorial series which can all be found on my profile tracking page.
User Page Editing:
For the userPage.php to know which user we are wanting to get the information from, we are going to be using a username variable through a get statement (within the URL). We will create a user list in a future tutorial, so for now; to test the page simply go to userPage.php?username= followed by a username who has posted a thread (userPage.php?username=admin) for example.
First we will check if a username is set and validated, then we will output each thread title that user has created in to a table variable named 'threads' along with a link to the threadPage showing the full thread...
<?php
$threads = '<table><tbody>';
if (isSet($_GET['username']) && $_GET['username'] != '') {
$user = $_GET['username'];
$q = mysqli_query($con, "SELECT * FROM `threads` WHERE `author`='$user'");
$threads .= '<tr><td><a href="threadPage.php?tid='.$row["id"].'">'.$row["title"].'</td></tr>';
}
$threads .= '</tbody></table>';
}else
$dontDisplay = true;
}
?>
We create a new variable called dontDisplay if the username is giving errors. We reference to this later to decide whether we want to display anything or not.
Displaying the Table:
Finally we need to output the threads table...
if (!isSet($dontDisplay)) {
echo "
<html>
<head></head>
<body>
$threads
</body>
</html>";
}else
echo 'That user does not exist!';
?>
!!! Section 3 !!!
Introduction:
In this tutorial we will be adding a messaging form for the logged in user to contact the specified user.
User Page Editing:
First we need the HTML form for the contact function...
<br/>
<h1>Contact</h1>
<form <?php echo 'action="userPage.php?username='.$user.'"'; ?> method='POST'>
<table>
<tbody>
<tr>
<td>Message:</td><td><input type='text' name='message' /></td>
</tr>
<tr>
<td></td><td><input type='submit' value='Send Message' name='messageSent' /></td>
</tr>
</tbody>
</table>
</form>
PHP:
Now for the actual sending PHP function/code. First we want to check if the messageSent post variable is set (that the submit button has been pushed), then we want to check and validate the title and message. If they are ok, we will add the information to our messages table within our forumTutorial database (created in the previous tutorial)...
if (isSet($_POST['messageSent']) && isSet($_POST['message']) && $_POST['message'] != '') {
$message = $_POST['message'];
$from = $_SESSION['username'];
$to = $_GET['username'];
$q = mysqli_query($con, "INSERT INTO `messages` VALUES ('', '$from', '$message', '$to')");
if ($q) {
echo 'Message sent.';
}else
echo 'Failed to send message, contact the site administrator.';
}