Function _asp Foundation of multi-field fuzzy search

Source: Internet
Author: User
Some time ago wrote a multiple-field fuzzy search function (similar to Baidui). The function code is as follows
'==================================================
' Function name: Keyword_sousuo
' function: Generate SQL query criteria
' Parameters: table_field------Table's field names (separated by commas)
' Keyword------search keywords (separated by spaces)
' Return: SQL query condition
'==================================================
Function Keyword_sousuo (ByVal table_field,byval Keyword)
Dim str01,str02,keywords,table_fields,i,j

Table_fields=split (Trim (Table_field), ",")
Keywords=split (Trim (keyword), ",")

If table_field<> "" Then
Str01= "(" &table_fields (0) & "like '%" &keyword& "%"
For j=0 to UBound (table_fields)
str01=str01& "or" &table_fields (j) & "like"% "&keyword&"% "
Next
str01=str01& ")"
Else
Response. Write ("<script>alert (" parameter error (cannot be empty)! ') </script> ")
Response. End ()
End If

' Full angle--' half corner space
' Keyword=replace (keyword, "", "")
' While INSTR (keyword, "") >0
' Keyword=replace (keyword, "", "")
' Wend
Keywords=split (keyword, "")
If UBound (keywords) >0 then
For i=0 to UBound (keywords)
str02=str02& "(" &table_fields (0) & "like"% "&keywords (i) &"% "
For J=1 to UBound (table_fields)
str02=str02& "or" &table_fields (j) & "like"% "&keywords (i) &"% "
Next
str02=str02& ")"
Next
Str02= "(" &replace (Str02, ") (", ") and (") & ")"
Keyword_sousuo= "(" & Str01 & "or" & Str02 & ")"
Else
Keyword_sousuo=str01
End If
End Function
'==================================================
' Function name: Keyword_tag
' Function: Mark the keyword in the string as red
' Argument: str------string
' keyword------tagged keywords (separated by spaces)
' Return: string (HTML format)
'==================================================
Function Keyword_tag (ByVal str,byval Keyword)
Dim keywords,str01,str02,i
' Full angle--' half corner space
' Keyword=replace (keyword, "", "")
' While INSTR (keyword, "") >0
' Keyword=replace (keyword, "", "")
' Wend
Str01=replace (Str,keyword, "<font color=" "#ff0000" ">" &keyword& "</font>")
Keywords=split (keyword, "")
If UBound (keywords) >0 then
Str02=str
For i=0 to UBound (keywords)
Str02=replace (Str02,keywords (i), "<font color=" "#ff0000" ">" &keywords (i) & "</font>")
Next
Keyword_tag=str02
Else
Keyword_tag=str01
End If
End Function
Here is an example http://www.zhouhaihe.com/blog/uploads/200604/recordread.asp

<% @LANGUAGE = "VBSCRIPT" codepage= "936"%>
<%
Option Explicit
Dim conn,connstr,database
Connstr= "dbq=" +server.mappath ("Record.mdb") + ";D efaultdir=;D river={microsoft Access DRIVER (*.mdb)};"
Set Conn=server. CreateObject ("Adodb.connection")
Conn.Open ConnStr

Dim n,rs,page,keyword,keywords,recordcount,pagecount,pos,i,sql_where
N=50
Page=request ("page")
Keyword=trim (Request ("keyword"))
Sql_where=keyword_sousuo ("title,cz,name,time", keyword)
Set Rs=server. CreateObject ("Adodb.recordset")
Rs.Open "SELECT * from record where" &sql_where& "ORDER BY id Desc", conn,1,2
If Page=0 Then
Page=1
End If
RecordCount = 0
Do but not Rs. Eof
RecordCount = RecordCount +1
Rs. MoveNext
Loop
If not recordcount=0 then
Rs. MoveFirst
End If
pagecount=recordcount/n
Pagecount=int (PageCount)
if (RecordCount mod n) >0 Then
Pagecount=pagecount +1
End If
%>
<style type= "Text/css" >
<!--
. STYLE3 {color: #666666}
-->
</style>
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" >
<form id= "Form1" Name= "Form1" "method=" post "action="? &GT;&LT;TR&GT;&LT;TD align= "center" ><input name= "keyword" type= "text" id= "keyword" value= "<%=keyword%>" Size= "/>"
<input type= "Submit" name= "submit" value= "Search"/>
<span class= "STYLE3" > (keywords separated by spaces) </span></td>

</tr></form>
<tr>
&LT;TD height= "5" ></td>
</table>

<table width= "border=" "1" align= "Center" cellpadding= "4" >
&LT;TR align= "center" >
&LT;TD width= "131" > title </td>
&LT;TD width= "131" > Action </td>
&LT;TD width= "131" > Username </td>
&LT;TD width= "132" > Time </td>
</tr>
<%pos=0
Do While pos< (page-1) *n
Pos=pos+1
Rs.movenext
Loop
I=0
While not rs.eof and I<n
I=i+1%>
&LT;TR align= "center" >
<td><%=keyword_tag (RS ("title"), keyword)%></td>
<td><%=keyword_tag (RS ("CZ"), keyword)%></td>
<td><%=keyword_tag (RS ("name"), keyword)%></td>
<td><%=keyword_tag (RS ("time"), keyword)%></td>
</tr>

<%rs.movenext
Wend
Rs.close
Set rs=nothing
Conn.close
Set conn=nothing

'==================================================
' Function name: Keyword_sousuo
' function: Generate SQL query criteria
' Parameters: table_field------Table's field names (separated by commas)
' Keyword------search keywords (separated by spaces)
' Return: SQL query condition
'==================================================
Function Keyword_sousuo (ByVal table_field,byval Keyword)
Dim str01,str02,keywords,table_fields,i,j

Table_fields=split (Trim (Table_field), ",")
Keywords=split (Trim (keyword), ",")

If table_field<> "" Then
Str01= "(" &table_fields (0) & "like '%" &keyword& "%"
For j=0 to UBound (table_fields)
str01=str01& "or" &table_fields (j) & "like"% "&keyword&"% "
Next
str01=str01& ")"
Else
Response. Write ("<script>alert (" parameter error (cannot be empty)! ') </script> ")
Response. End ()
End If

' Full angle--' half corner space
Keyword=replace (keyword, "", "")
' While INSTR (keyword, "") >0
' Keyword=replace (keyword, "", "")
' Wend
Keywords=split (keyword, "")
If UBound (keywords) >0 then
For i=0 to UBound (keywords)
str02=str02& "(" &table_fields (0) & "like"% "&keywords (i) &"% "
For J=1 to UBound (table_fields)
str02=str02& "or" &table_fields (j) & "like"% "&keywords (i) &"% "
Next
str02=str02& ")"
Next
Str02= "(" &replace (Str02, ") (", ") and (") & ")"
Keyword_sousuo= "(" & Str01 & "or" & Str02 & ")"
Else
Keyword_sousuo=str01
End If
End Function
'==================================================
' Function name: Keyword_tag
' Function: Mark the keyword in the string as red
' Argument: str------string
' keyword------tagged keywords (separated by spaces)
' Return: string (HTML format)
'==================================================
Function Keyword_tag (ByVal str,byval Keyword)
Dim keywords,str01,str02,i
' Full angle--' half corner space
Keyword=replace (keyword, "", "")
' While INSTR (keyword, "") >0
' Keyword=replace (keyword, "", "")
' Wend
Str01=replace (Str,keyword, "<font color=" "#ff0000" ">" &keyword& "</font>")
Keywords=split (keyword, "")
If UBound (keywords) >0 then
Str02=str
For i=0 to UBound (keywords)
Str02=replace (Str02,keywords (i), "<font color=" "#ff0000" ">" &keywords (i) & "</font>")
Next
Keyword_tag=str02
Else
Keyword_tag=str01
End If
End Function
Dim y
%> <tr align= "center" >
&LT;TD colspan= "4" > <%=RecordCount%> <font color= "#FF0000" ><%=page%></font>/<%= Pagecount%> page
<%=n%>/page turn to:
<select name= "SELECT" Onchange=window.open (This.options[selectedindex].value, ' _self ') >
<%y=1
Do While y<pagecount+1%>
<option Value=?page=<%=y%>&keyword=<%=server. UrlEncode (keyword)%> <%if CStr (page) =cstr (y) then response. Write ("selected") End if%> > <%=y%> page </OPTION>
<%y=y+1
Loop%>
</SELECT></td>
</tr>
</table>

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.