ASP to achieve the same search Baidu keyword highlighting effect

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.


The first step is to create a database called Db_sample.mdb (this article takes the Access2000 database for example) and creates a table t_sample. Table T_sample includes the following fields:
ID Auto Number
U_name text
U_info notes
In the second step, we begin to design the search page search.asp. This page includes a form (Frm_search), which includes a text box and a Submit button. 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= "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 Opening a database connection
%>

Next, determine the data that the ASP page receives and search in the database.
<font color= "#FF0000" > did not find any results!!! </font>
<%
Else
%>

Search for items named "<font color=" #FF0000 "><%= s_key%></font>" for a total of <font color= "#FF0000" ><%= RST. RecordCount%></font> Items:
<%
While not RST. EOF traverses the entire recordset, displays the search information and sets the link
%>
<!--can be set to 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" ><%= 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

' 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, 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.
<%
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.



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.