Pages

Friday, April 23, 2010

Simple Search Solution

Though not as powerful as Google, there is an easy way to program a search function that will allow visitors to easily search through files and posts on a website. This is especially handy for websites that post blogs, or upload video and audio files.

For a basic search function, there only needs to be two items in the form: an text field and a submit button. To keep things simple, have the form submit to itself with the results being display below the form.

Once the form is built in HTML, the next step is to decide what field will be searched. Let's say that this is for a website that was designed for a church so they can share their services online. Their database fields include the location of the file, the title, who spoke, and a brief description. The best field here to be used to search would be the description field.

Now we start on the PHP stuff. The first step is to take the text from the input field and store it in a variable. Next, we 'explode' it, saving the result to a new variable. Explode is a PHP function with two parameters. The first defines the string that the function will use to divide up our search text into an array. Usually, this will be a space (" "). Next, for user friendliness later on, create a counter variable and set it to zero. This will come in handy later.

No we are ready to start the search. First, use SQL get every item in the table. This is done like this:

$fetch_sql = mysqli_query($link, "SELECT * FROM mp3");

$link in this case is the variable that stores the connection to the database. Next, use a while loop to cycle through every item that we just got:

while($r = mysqli_fetch_array($fetch_sql)) { 
extract($r);

Now that we have entered this loop, the next thing we need to get is the description, which the extract function will automatically put into the variable $description. Because there is a chance that there will be punctuation in the description field, we need to remove them. Otherwise, they might interfere with the search. The best way to do this is this:

$description = str_replace(".", "", $description);

This will remove all periods in the description. Though a little time consuming, just copy and paste this and replace the period with a comma, colon, and so on. Once that is done, use the explode function again to divide up all the words into an array.

There is one more thing that has to be done before we continue. Create a variable $found and set it to false. This will help prevent duplication of search results.

Now we're ready to enter into more loops. These loops will be for loops, which will cycle each word we entered into the search field, and match it to each word in the description of this particular entry in the database.

for ($x=0; $x < count($search[$x]); $x++) { 
for ($y=0; $y < count($description[$y]); $y++) { 
if (strtolower($search[$x]) == strtolower($description[$y])) { 
if (!$found) { 
// this is where you echo the result 
?> 

Title: 
Speaker: 
MP3: Click Here

$found = true; 
$count++; 



}

Make sure that in the code above, both strings are being changed to lower case, and if a match is found, that the $found variable is set to true. Otherwise, if multiple words match the description, duplicate results will be displayed. Also, make sure to increase the $count variable's amount for each displayed match. That way, if the result comes up zero, the user will see a message saying that no results were found.

ShareThis

No comments:

Post a Comment