ASP realizes the website intelligence participle search

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


first step, we're going 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. Table T_sample includes the following fields:





ID Automatic Number


u_name Text


U_info Notes





The second step, we begin 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 code fragment:


!--search.asp-->


<form name= "Frm_search" method= "Get" action= "search.asp" >


Please enter the keyword:


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








The following code fragment:


<%


Dim strprovider,cnn


strprovider= "Provider=Microsoft.Jet.OLEDB.4.0;Data source="


Strprovider=strprovider &amp; Server.MapPath ("") &amp; "Datadb_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 the database connection


%>








Next, determine the data that the ASP page receives and search in the database.





The following code fragment:


<font color= "#FF0000" did not find any results!!! </font>


<%


Else


%>


Search for items named "<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, 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 code above, there is a custom function AutoKey, which is the core of the implementation of intelligent search. The code is as follows:








The following code fragment:


<%


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 the string is empty, go to the 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 a query condition


for I=1 to lnglenkey-(lngSubKey-1)


Strsubkey=mid (Strkey,i,lngsubkey)


Strnew1=strnew1 &amp; "or u_name like%" &amp; strSubKey &amp; "%"


Strnew2=strnew2 &amp; "or u_info like%" &amp; strSubKey &amp; "%"


Next


End Select


' Gets the complete SQL statement





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


End Function


%>





to implement intelligent search, the core is to automatically group search keywords. 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 code fragment:


<%


CNN. Close


Set cnn=nothing


%>





So, this


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.