Implementation of ASP Intelligent Search

zhaozj2021-02-17  53

Implementation of ASP Intelligent Search

It is very convenient to use ASP to implement the function of the search engine, but how do I achieve smart search similar to 3721? For example, when entering the "Chinese people" in the search condition box, the keyword "China", "People" and "People" are automatically extracted and search in the database. After reading this article, you can find that this feature is actually so simple. OK, FOLLOW ME! The first step, we have to build a database called DB_SAMPLE.MDB (this article is used as an example of Access2000 database) and sets table t_sample. Table T_SAMPLE includes the following fields: ID Auto Number U_NAME Text U_INFO Remarks Step 2, we start designing search page search.asp. This page includes a form (fRM_SEARCH), including a text box and a submission button. And set the form of the form to "get", the Action property is set to "Search.asp", that is, submit it to the web. The code is as follows:

Please enter the keyword: below, enters the key part of the intelligent search. First, establish a database connection. The following code is added to the beginning of Search.asp: <% DIM STROVIDER, CNN STROVIDER = "provider = microsoft.jet.OleDb.4.0; data source =" strprovider = strprovider & server.mappath ("/") & "/ data /db_sample.mdb "Hypothesis data inventory is placed in the main page root directory SET CNN = Server.createObject (" adodb.connection ") cnn.open strprovider 'Open Database Connection%> Next, determine that the ASP page receives Data and search in the database.

<% DIM S_KEY, RST, STRSQL S_KEY = TRIM (Request ("Key") 'Get Search Keyword Value IF S_KEY <> "" "THEN SET RST = Server.createObject (" AdoDb.Recordset ") strsql = autoKey S_key) 'Use custom functions automoKey () here, which enables the core Rst.open Strsql, CNN, 3, 2' to achieve smart search, to find the search IF RST.BOF AND RST.EOF THEN%> Did not find any results! ! ! <% else%> Search name " <% = s_key%> " found, found <% = RST "U_name")%>
<% = left (RST ("u_info") 150)%>

<% RST.MOVENEXT Wend Rst.Close Set RST = Nothing End if End if%> In the above code, there is a custom function AutoKey, which is intelligent search The core is located. The code is as follows: <% function autoKey (strkey) const LNGSubkey = 2 Dim Lnglenkey, strnew1, strnew2, i, strsubkey 'Detecting the legality of the string, if not legal, go to an error page.

Error page You can set 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 (34)) <> 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 an empty string, go to the error page response.Redirect "error.htm" case 1' If the length is 1, no value strnew1 = "" strnew2 = "" Case Else 'is greater than 1, Starting from the string first character, the sub-string of the length of 2 is used as a query condition for i = 1 to lnglenkey- (lngsubkey-1) strsubkey = mid (strkey, i, lngsubkey strnew1 = strnew1 & "or u_name like Like '% "& strsubkey &"%' "strnew2 = strnew2 &" or u_info like '% "& strsubkey &"%' "NEXT End SELECT 'Get full SQL statement autokey =" select * from t_sample where u_name like "%" & st Rkey & "% 'or u_info like'%" & strkey & "% '" & strnew1 & strnew2 end function%> To achieve smart search, its core is to automatically packet search keywords. Here, we use a method of recirculating a substring having a length of 2. Why don't you set a substring length to 1, 3, 4 or others? This is because if the seasse string length is less than 2, it will lose the function of the keyword packet, while the son string is greater than 2, and some phrase is lost. Everyone can change the const lcomubkey = 2 to other numbers to try a try, and the exception is derived.

转载请注明原文地址:https://www.9cbs.com/read-29311.html

New Post(0)