ASP Search Engine Script - ASP Web Pro
  Active Server Pages Programming by ASP Web Pro

ASP Scripts

SEARCH ENGINE

So when would you need a search engine on your website. Well, one popular example is an FAQ (frequently asked questions) database. You could merely list all of your FAQ's on one page, but if you have a lot of them like the Micsoft Knowledge Base, then it would be a good idea to let your users search your database by keyword or phrase.

The first thing you need to do is create a database called MyDatabase. Then create a table called tblFAQ with these fields:
ID - autonumber
Question - text field
Answer - text field
DateEntered - date/time field

Then, start adding some questions and answers to your database.

Next, create a page called searchengine.asp and copy the below code into your page:

<!--#INCLUDE VIRTUAL="/includes/connection.asp" -->

<form name="search" method="post" action="searchresults.asp">
<table width="100%" border="1">
<tr><td><div align="center">Search our FAQ's</div></td></tr>
<tr><td>Enter Search Term</td></tr>
<tr><td><input type="text" name="searchterm">
<input type="submit" name="Submit" value="Search">
</td></tr></table></form>

<%
DIM strSearchterm
strSearchterm = Request.Form("searchterm")

IF strSearchterm <> "" THEN
DIM mySQL, objRS
mySQL = "SELECT * FROM tblFAQ WHERE (Question LIKE '%" & strSearchterm & "%') OR (Answer LIKE '%" & strSearchterm & "%')"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn, adOpenKeyset, adLockPessimistic, adCmdText

IF objRS.EOF THEN
Response.Write "Sorry, no records were found for that searchterm"
ELSE
%>

<table width="100%">
<tr><td>Question</td><td>Answer</td></tr>
<% DO WHILE NOT objRS.EOF %>
<tr><td><%=objRS("Question")%></td><td><%=objRS("Answer")%></td></tr>
<%
objRS.MoveNext
Loop

END IF
%>
</table>

<%
ELSE
' No searchterm submitted, display blank
END IF
%>


This script will display your search form each time the page is accessed. When a search term is submitted, it will search both the question and answer fields in your database for any data that contains the search term. There you have it. Your first albeit simple search engine.

< Back to ASP Scripts


 

 

Main Menu
Home
ASP Scripts
ASP Tutorials
JavaScripts
Awards
Contact Us

Email This Page

Other Resources
ASP Web Hosting
Search Engine Submission
Programming Help

 

 

 

Other ASP Web Sites

Site Map

 

Last Updated:
06 July 2008