PHP/MySQLi Creating a Forum - Part 9 - Tags #2

PHP/MySQLi Creating a Forum - Part 8 - Tags #1 Introduction: This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. In this ninth part, we are going to be continuing a tag system which will allow the user to search for threads specific to their needs. This time we are going to be letting the user search for threads with the given tags, so I guess this is also adding a search function to the website in general. 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 and eight parts of this tutorial series which can all be found on my profile tracking page. Recap: To recap from the previous tutorial, we have just added the tag column on to the end of our threads table. We also edited the thread creation form to allow the user to input some tags when creating the thread. Editing Thread List Page: Again, before we do anything in the PHP/MySQLi side of things, we need to add an actual search form in to our thread list page. So, on forumTutorial.php we are going to add this to the top of the page...
  1. <h1>Search Threads:</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>
searchThreads.php Creation: As you can see from the form above, I have made it so it sends the information to a new page named searchThreads.php. In there, we are going to; Validate the information Convert the query string to lower case (to match tags from the table later on) Split the query string up in to a string array (by space) Get every row from the threads table within the database For each row we are going to convert the tags to lower case, and then check if the entire tags field contains any of the searched query words.
  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysql_error());
  3. $returns = '<table><tbody>';
  4. if (isSet($_POST['searchSent']) && isSet($_POST['searchQuery']) && $_POST['searchQuery'] != '') {
  5. $s = $_POST['searchQuery'];
  6. $s = strtolower($s);
  7. $ss = explode(' ', $s);
  8. $q = mysqli_query($con, "SELECT * FROM `threads`");
  9. if (mysqli_num_rows($q) > 0) {
  10. while ($row = mysqli_fetch_array($q)) {
  11. $hasTag = false;
  12. if ($row['tags'] != '') {
  13. $tags = strtolower($row['tags']);
  14. for ($i=0;$i<count($ss);$i++) {
  15. if (strpos($tags, $ss[$i]) !== false) {
  16. $hasTag = true;
  17. }
  18. }
  19. }
  20. if ($hasTag) {
  21. $returns .= '<tr><td><a href="threadPage.php?tid='.$row["id"].'">'.$row["title"].'</a></td></tr>';
  22. }
  23. }
  24. }
  25. }
  26. ?>
  27. <html>
  28. <head></head>
  29. <body>
  30. <?php echo $returns; ?>
  31. </body>
  32. </html>

Add new comment