PHP/MySQLi Creating a Forum - Part 12 - User Page #2 - User Created Threads

PHP/MySQLi Creating a Forum - Part 11 - User Page #1 - Messaging Database & User Page Basics 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...
  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'forumTutorial');
  3. $threads = '<table><tbody>';
  4. if (isSet($_GET['username']) && $_GET['username'] != '') {
  5. $user = $_GET['username'];
  6. $q = mysqli_query($con, "SELECT * FROM `threads` WHERE `author`='$user'");
  7. if (mysqli_num_rows($q) > 0) {
  8. while ($row = mysqli_fetch_array($q)) {
  9. $threads .= '<tr><td><a href="threadPage.php?tid='.$row["id"].'">'.$row["title"].'</td></tr>';
  10. }
  11. $threads .= '</tbody></table>';
  12. }
  13. }
  14. ?>
Displaying the Table: Finally we need to output the threads table...
  1. <html>
  2. <head></head>
  3. <body><?php echo $threads; ?></body>
  4. </html>

Add new comment