PHP/MySQLi Creating a Forum - Part 19 - User Search

PHP/MySQLi Creating a Forum - Part 18 - Related Threads Introduction: This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. In this part I will be covering searching for users. 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 all the previous parts of this tutorial series which can all be found on my profile tracking page. User List: Since we made a user list, I thought we might as well add user search to our thread search. You could create a whole new search purely for users but I am just going to add it in to the current thread search that we have. Theory: Essentially, we are just going to do the exact same as the thread search, except change the revelant information for the users table and modify the output a bit to userPage instead of threadPage. Editing forumTutorial.php: Before I do anything with the search itself, I am going to change the text above the search form from "Search Threads:" to "Search:"...
  1. <h1>Search:</h1>
  2. <form action='searchThreads.php' method='POST'>
  3. <table border='0'>
  4. <tbody>
  5. <tr>
  6. <td><input type='text' name='searchQuery' /></td>
  7. </tr>
  8. <tr>
  9. <td><input type='submit' value='Search' name='searchSent' /></td>
  10. </tr>
  11. </tbody>
  12. </table>
  13. </form>
Editing searchThreads.php: Although the file name is now wrong, I'm not going to edit it since it doesn't really affect anything. Next I want to edit the source code of searchThreads.php, first I am going to organise the html output of the page...
  1. <h1>Threads:</h1>
  2. <?php echo $returns; ?>
  3. <br/>
  4. <h1>Users:</h1>
Although we haven't created it yet, just under the Users header, we are going to output the PHP variable table of users using...
  1. <?php echo $foundUsers; ?>
Now we need to create our PHP variable table (outside of any blocks)...
  1. $foundUsers = '<table><tbody>';
Next we want to add the user search just underneath the thread search...
  1. $sQ = $_POST['searchQuery'];
  2. $uQ = mysqli_query($con, "SELECT * FROM `users` WHERE `username`='$sQ'");
  3. if (mysqli_num_rows($uQ) > 0) {
  4. while ($row = mysqli_fetch_array($uQ)) {
  5. $user = $row['username'];
  6. $foundUsers .= '<tr><td><a href="userPage.php?username='.$user.'">'.$user.'</a></td></tr>';
  7. }
  8. }
The above code just gets all the users from our database which have the same name as the search query (it should be one at the most). We then iterate through each one (hopefully no more than one!) and output each one with a link to their profile. And finally we need to end the table...
  1. $foundUsers .= '</tbody></table>';

Add new comment