Wednesday, 30 November 2011

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:



Search our FAQ's
Enter Search Term



<% 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
%>

<% DO WHILE NOT objRS.EOF %> <% objRS.MoveNext Loop END IF %>
QuestionAnswer
<%=objRS("Question")%><%=objRS("Answer")%>

<% 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.

http://www.aspwebpro.com/aspscripts/websitetools/searchengine.asp

No comments:

Post a Comment