PHP/MySQLi Creating a Forum - Part 8 - Tags #1
Submitted by Yorkiebar on Wednesday, January 15, 2014 - 03:56.
PHP/MySQLi Creating a Forum - Part 7 - Subscriptions #2
Introduction:
This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. In this eighth part, we are going to be starting a tag system which will allow the user to search for threads specific to their needs.
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 and seventh parts of this tutorial series which can all be found on my profile tracking page.
Database Editing:
Before we can start making this new tag feature, we are going to add the column 'tags' to the end of the 'threads' table...
tags - VARCHAR - 255 Length
Editing Thread Creation:
Before we do anything else, we need to modify our thread creation process in order to take in to account the extra column. To do this, we are going to add another text input box to our form...
Next we are going to move to the part where we process the form. First we will check if the tags variable is set and not nothing, then we are going to put the value of the tags input form in to a new 'tags' variable, then convert the whole string in to lower case (so we can easily determine the tags from one another so the user can later search with any capitalization to get the same results for keywords/tags)...
Add this to the checks:
Then, just above where we set the $username variable equal to our $_SESSION['username'] variables:
Finally we want to insert the tags in to the final column of our table, we need to modify the mysqli query to do this...
One Last Thing...
Before I end the tutorial, I should just mention how it is going to work...
The tags get added to the database on the thread creation (separated by a comma), then we are going to give the user an option to search for threads later which will get the tags value for each row within our threads table, remove all the spaces, split up the tags by a comma and then check if any of the tags for each row match the search criteria... get it? If not, send me a message and I'll help you understand any of my tutorials.
We should also just let tbe user know to separate the tags by a comma...
- <tr>
- <td>Description: </td><td><input type='text' name='description' /></td>
- </tr>
- $tags = $_POST['tags'];
- $q = mysqli_query($con, "INSERT INTO `threads` VALUES ('', '$title', '$description', '$user', '0', '0', '$tags')") or die(mysql_error());
- <tr>
- <td>Tags: </td><td><input type='text' name='tags' /></td><td>Tags must be separated by a comma (,)</td>
- </tr>
Add new comment
- 59 views