ASP Intelligent Search Implementation _ Application Skills

Source: Internet
Author: User
Using ASP to achieve the function of the search engine is a very convenient thing, but how to achieve a similar 3721 intelligent search
Where's the rope? For example, when entering "Chinese people" in the search conditions box, automatically extracts "China" from the
"People" and other keywords and search in the database. After reading this article, you can find that this work
It's so simple to realize. Ok,follow me!
The first step is to create a database called Db_sample.mdb (this article takes the number of Access2000
According to the library as an example), and to establish a table t_sample. 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. This page includes a form
(Frm_search), a text box and a Submit button are included in the form. And the method of the form
Sex is set to "get" and the Action property is set to "Search.asp", which is submitted to the 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= "ten" >
<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" assumes that the database resides in the data directory under the home page root directory
Set CNN = Server.CreateObject ("Adodb.connection")
Cnn. Open strprovider ' opens the 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 (), the letter
Number is the core of 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 name is "<font color=" #FF0000 "><%= s_key%
></font> ", a total of <font color=" #FF0000 "><%= RST was found. RecordCount%
></font> Item:<p>
<%
While not RST. EOF ' traverses the entire Recordset, showing the search to the letter
and set the link
%>
<!--can be set to the link target you need-->
<font style= "font:12pt song Body" ><a
Href= "info.asp?id=<%= rst (" ID ")%>" target= "_blank" ><%= RST ("U_name")
%></a></font><br>
<!--display part details-->
<font style= "font:9pt song Body" ><%= 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 kernel of implementing intelligent search
Where the heart is. The code is as follows:
<%
Function AutoKey (strkey)
CONST lngsubkey=2
Dim Lnglenkey, StrNew1, StrNew2, I, strSubKey

' Check the legality of the string, if not legitimate, go to the error page. Error page you can do what you need to
be set.
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, no value is set
Strnew1= ""
Strnew2= ""
Case Else ' If the length is greater than 1, start with the first character of the string, and the loop takes the length
Substring of 2 as a 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 complete 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. At this point, we make
A method of looping a substring with a length of 2 is used. Why not set the substring length to 1, 3, 4, or something else?
This is because the Jozo string is less than 2 or 1 o'clock, losing the ability to group the keywords, while the Jozo string length
is greater than 2, some phrases are lost. You can change the CONST lngsubkey=2 to a different number. Try it.
, it's better to see what's better.
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 refine it, such as adding points
Features such as page, highlight, and so on. 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.