ASP realizes the website intelligence participle search

Source: Internet
Author: User
Tags end key string root directory
Using ASP to achieve the function of the search engine 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.

Using ASP to achieve the function of the search engine 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:


The following is a code fragment:
!--search.asp-->

Please enter the key word:

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:


The following is a code fragment:
<%
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.

The following is a code fragment:
no results found!!!   
<%  
else  
%>  
Search name is "<%= S _key%> "to find a total of <%= RST. RecordCount%> Entry:

  
<%  
While not RST. EOF traverses the entire recordset, displays the search information and sets the link   
%>  
!--here to set the link target you want-->  
"target=" _blank "><%= RST (" U_name ")%>   
!--Display section details-->  
<%= Left (RST ("U_info"),%>

  
<%   
RST. movenext  
wend  
RST. close  
Set rst=nothing  
End if  
End if  
%>
in the previous generation Code, there is a custom function AutoKey, the function is the core of the implementation of intelligent search. The code is as follows:


The following is a code fragment:
<%
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. 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 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. 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.


The following is a code fragment:
<%
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.