Add a smart Search to an ASP application

Source: Internet
Author: User
Tags add end key string root directory
The application of ASP to implement search engine function is a very convenient thing, but, how to achieve a similar 3721 intelligent search? For example, when you enter the "Chinese people" in the search conditions box, automatically extracts "China", "people" and other keywords and search in the database. After reading this article, you will find that this function is so simple to achieve.
The first step is to create a database called Db_sample.mdb (this article takes the Access2000 database as an example) and creates a table t_sample in it. The table T_sample includes the following fields:

ID Auto Number
U_name text
U_info notes

The second step, we start to design the search page search.asp. The page includes a form (Frm_search) that includes a text box and a Submit button in the form. The form's Method property is set to "get" and the Action property is set to "Search.asp", which is submitted to the Web page itself. The code is as follows:

!--search.asp-->
<form name= "Frm_search" method= "Get" action= "Search.asp"
Please enter the key word:
<input type= "text" name= "key" size= "10"
<input type= "Submit" value= "Search"
</form>
Below, you are entering a key part of implementing intelligent search.

First, establish a database connection. Add the following code at the beginning of the Search.asp:

<%
Dim STRPROVIDER,CNN
strprovider= "Provider=Microsoft.Jet.OLEDB.4.0;Data source="
Strprovider=strprovider & Server.MapPath ("\") & "\data\db_sample.mdb" assumption database is stored in the data directory under the home page root directory
Set CNN = Server.CreateObject ("Adodb.connection")
Cnn. Open strprovider Opening a database connection
%>
Next, determine the data that the ASP page receives and search in the database.

<%
Dim S_key,rst,strsql
S_key = Trim (Request ("key") gets the value of the search keyword
If s_key <> "" Then
Set rst=server.createobject ("ADODB. RecordSet ")
Strsql=autokey (S_key) here uses the Custom function AutoKey (), which is the core of the intelligent search
Rst. Open strsql,cnn,3,2 gets the record after the search
If RST. BOF and RST. EOF Then
%>
<font color= "#FF0000" did not find any results!!! </font>
<%
Else
%>
Search for items with the name "<font color=" #FF0000 "><%= s_key%> </font>" for a total of <font color= "#FF0000" ><%= RST. RecordCount%> </font> Items: <p>
<%
While not RST. EOF traverses the entire recordset, displays the search information and sets the link
%>
!--Here you can set the link target you need-->
<font style= "font:12pt Arial ><a href=" info.asp?id=<%= rst ("ID")%> "target=" _blank "><%= rst (" U_name ")%> </a> </font>

!--Display Part Details-->
<font style= "font:9pt Arial" ><%= Left (RST ("U_info"),%> </font> <p>
<%
Rst. MoveNext
Wend
Rst. Close
Set rst=nothing
End If
End If
%>
In the above code, there is a custom function AutoKey, which is the core of the implementation of intelligent search. The code is as follows:

<%
Function AutoKey (strkey)
CONST lngsubkey=2
Dim Lnglenkey, StrNew1, StrNew2, I, strSubKey
Detects the legality of the string and goes to the error page if it is not valid. You can set the error page as needed.

If InStr (strkey, "=") <> 0 or InStr (strkey, "'") <> 0 or InStr (strkey, "") <> 0 or InStr (strkey, "") <> 0 or InStr (strkey, "") <> 0 or INSTR (Strkey, "") <> 0 or InStr (STRKEY,CHR) <> 0 or InStr (strkey, "\") <> 0 or InStr (strkey, ",") <> 0 or InStr (strkey, "") <> 0 or InStr (strkey, ">") <> 0 Then
Response.Redirect "Error.htm"
End If

Lnglenkey=len (strkey)
Select Case Lnglenkey
Case 0 If empty string, go to error page
Response.Redirect "Error.htm"
Case 1 If the length is 1, then no value is set
Strnew1= ""
Strnew2= ""
Case Else If the length is greater than 1, start with the first character of the string, and iterate over the substring of length 2 as the query condition
For I=1 to lnglenkey-(lngSubKey-1)
Strsubkey=mid (Strkey,i,lngsubkey)
Strnew1=strnew1 & "or u_name like%" & strSubKey & "%"
Strnew2=strnew2 & "or u_info like%" & strSubKey & "%"
Next
End Select
Get the full SQL statement

Autokey= "Select * from T_sample where u_name like%" & strkey & "% or U_info like%" & strkey & "%" & StrNew1 & StrNew2

End Function
%>
The core of intelligent search is to group search keywords automatically. Here, we use a method of looping through a substring of length 2. Why not set the substring length to 1, 3, 4, or something else? This is because the Jozo string length less than 2 is 1 o'clock, will lose the ability to group the keyword, and the Jozo string length is greater than 2, you will lose some phrases. You can change the CONST lngsubkey=2 to other numbers to try it out.

Finally, don't forget to close the data connection to free up resources.

<%
Cnn. Close
Set cnn=nothing
%>
So far, this intelligent search engine has been completed. You can also continue to improve it, such as adding pagination, highlighting and other functions. Well, don't delay everyone's time, go and try it quickly.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.