Keyword Based Search Engine

By Harry →
 This is a simple tutorial to help you create a simple PHP based search engine for your site.
Firstly, open up notepad and create a new html document. Type the following:

<html>
<head>
<title>Search</title>
</head>
<body>
<form method="post" action="results.php" id="search">
<input type="text" class="searchForm" name="search" value="search..." onfocus="if(this.value=='search...')this.value=''" onblur="if(this.value=='')this.value='search...'" />
</form>
</body>
</html>

This creates a form with a search field which will pass information onto the page "results.php".

Now to create "results.php", open Notepad again and type the following:

<?php
$term = $_POST['search'];
?>
<html>
<head>
<title>Search Results for &quot;<?php echo $term; ?>&quot;</title>
</head>
<body>
<?php

$term_temp = strtolower($term);
echo "<h1>Search Results for &quot;$term&quot;</h1>";

?>
<?php

if ($term_temp == "") {
echo "Please enter a search query.";
}

?>
<?php
//Home Page
switch ($term_temp) {
case "welcome":
case "home":
case "homepage":
case "design":
echo '<a href="index.html">Home</a><br />';

break;
}

?>
<?php
//About Page
switch ($term_temp) {
case "about":
case "us":
case "our work":
case "work":
case "w3c":
case "design":
echo '<a href="about.html">About</a><br />';

break;
}
?>
<?php
//Services Page
switch ($term_temp) {
case "services":
case "employ":
case "our work":
case "web":
case "work":
case "design":
case "code":
case "php":
case "css":
case "html":
case "xhtml":
echo '<a href="services.html">Services</a><br />';

break;
}
?>
<?php
//Contact Page
switch ($term_temp) {
case "contact":
case "call":
case "email":
case "phone":
case "quote":
case "discuss":
case "price":
case "prices":
echo '<a href="contact.html">Contact</a><br />';

break;
}
?>
<?php
//Swear Words!
switch ($term_temp) {
case "#!$@":
case "#!$@":
case "#!$@":
case "#!$@":
case "#!$@":
case "#!$@":
case "#!$@":
echo 'Ey! We\'ll have a bit less of that!';

break;
}
?>
</body>
</html>

Let's take a look at the script.
The first part gets the search term from the previous page and appears before even the <html> tag. This is so the search term can be used in the page title.
The next part, "<title>Search Results for &quot;<?php echo $term; ?>&quot;</title>" will show the title including the search term.

After this, the string is put into a new variable called term_temp which is a lowercase version of the original search term (strtolower($term);). This means that the search term hOMe would return the same result as home, HoME homE etc.

After this, a script checks that the search string isn't empty. If it is empty a message is displayed asking the user to input a search term.

Following this, the actual searching functions come in. Using the switch function the script checks whether a word is related to a particular page. "switch ($term_temp)" begins a comparison between the users term, and a list of predefined keywords. The case function basically means in the case of this, do this. If the user inputs "home" the script will echo "<a href="index.html">Home</a><br />", a hyperlink to the home page. The script carries on in this fashion for each page you wish to be indexed, with a list of keywords (which can be as long as you wish) and a resulting hyperlink. You can even add a list of profanities with a phrase to output for the more obscene user!

I have a working example of this search engine here.

P.S. This is a fairly primitive search engine and has obvious flaws. If a user types a word/phrase that isn't in the list, they don't get a result. However, by you defining the keywords, you can drive users to a certain page by what they type. It is not a script recommended for large websites.

Guest poster for the Graphic Design Blog

Post Tags:





Toon Creffield

Graphic and Digital Designer from Sheffield England and owner of the Estetica Design Forum.

No Comment to " Keyword Based Search Engine "