手机
当前位置:查字典教程网 >编程开发 >ASP教程 >多字段模糊搜索的函数
多字段模糊搜索的函数
摘要:前段时间写了一个多字段模糊搜索的函数(类似baidui)。函数代码如下'==================================...

前段时间写了一个多字段模糊搜索的函数(类似baidui)。函数代码如下

'==================================================

'函数名:keyword_sousuo

'作用:生成sql查询条件

'参数:table_field------表的字段名(之间用逗号分开)

'keyword------搜索关键词(之间用空格分开)

'返回:sql查询条件

'==================================================

functionkeyword_sousuo(byvaltable_field,byvalkeyword)

dimstr01,str02,keywords,table_fields,i,j

table_fields=split(trim(table_field),",")

keywords=split(trim(keyword),",")

iftable_field<>""then

str01="("&table_fields(0)&"like'%"&keyword&"%'"

forj=0toubound(table_fields)

str01=str01&"or"&table_fields(j)&"like'%"&keyword&"%'"

next

str01=str01&")"

else

response.Write("<script>alert('参数错误(不能为空)!')</script>")

response.End()

endif

'全角--》半角空格

'keyword=replace(keyword,"","")

'whileInStr(keyword,"")>0

'keyword=replace(keyword,"","")

'wend

keywords=split(keyword,"")

ifubound(keywords)>0then

fori=0toubound(keywords)

str02=str02&"("&table_fields(0)&"like'%"&keywords(i)&"%'"

forj=1toubound(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

endif

endfunction

'==================================================

'函数名:keyword_tag

'作用:将字符串里的关键词标记为红色

'参数:str------字符串

'keyword------标记关键词(之间用空格分开)

'返回:字符串(html格式)

'==================================================

functionkeyword_tag(byvalstr,byvalkeyword)

dimkeywords,str01,str02,i

'全角--》半角空格

'keyword=replace(keyword,"","")

'whileInStr(keyword,"")>0

'keyword=replace(keyword,"","")

'wend

str01=replace(str,keyword,"<fontcolor=""#ff0000"">"&keyword&"</font>")

keywords=split(keyword,"")

ifubound(keywords)>0then

str02=str

fori=0toubound(keywords)

str02=replace(str02,keywords(i),"<fontcolor=""#ff0000"">"&keywords(i)&"</font>")

next

keyword_tag=str02

else

keyword_tag=str01

endif

endfunction

以下是一个例子http://www.zhouhaihe.com/blog/uploads/200604/recordread.asp

<%@LANGUAGE="VBSCRIPT"CODEPAGE="936"%>

<%

optionexplicit

dimconn,connstr,database

connstr="DBQ="+server.mappath("record.mdb")+";DefaultDir=;DRIVER={MicrosoftAccessDriver(*.mdb)};"

setconn=server.CreateObject("adodb.connection")

conn.openconnstr

dimn,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)

setrs=server.CreateObject("adodb.recordset")

rs.open"select*fromrecordwhere"&sql_where&"orderbyiddesc",conn,1,2

ifpage=0then

page=1

endif

RecordCount=0

dowhilenotrs.Eof

RecordCount=RecordCount+1

rs.MoveNext

loop

ifnotRecordCount=0then

rs.MoveFirst

endif

pageCount=RecordCount/n

pageCount=int(pageCount)

if(RecordCountmodn)>0then

PageCount=PageCount+1

endif

%>

<styletype="text/css">

<>

</style>

<tablewidth="100%"border="0"cellspacing="0"cellpadding="0">

<formid="form1"name="form1"method="post"action="?"><tr><tdalign="center"><inputname="keyword"type="text"id="keyword"value="<%=keyword%>"size="30"/>

<inputtype="submit"name="Submit"value="搜索"/>

<spanclass="STYLE3">(关键词之间用空格分开)</span></td>

</tr></form>

<tr>

<tdheight="5"></td>

</table>

<tablewidth="600"border="1"align="center"cellpadding="4">

<tralign="center">

<tdwidth="131">标题</td>

<tdwidth="131">动作</td>

<tdwidth="131">用户名</td>

<tdwidth="132">时间</td>

</tr>

<%pos=0

dowhilepos<(page-1)*n

pos=pos+1

rs.moveNext

loop

i=0

whilenotrs.eofandi<n

i=i+1%>

<tralign="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

setrs=nothing

conn.close

setconn=nothing

'==================================================

'函数名:keyword_sousuo

'作用:生成sql查询条件

'参数:table_field------表的字段名(之间用逗号分开)

'keyword------搜索关键词(之间用空格分开)

'返回:sql查询条件

'==================================================

functionkeyword_sousuo(byvaltable_field,byvalkeyword)

dimstr01,str02,keywords,table_fields,i,j

table_fields=split(trim(table_field),",")

keywords=split(trim(keyword),",")

iftable_field<>""then

str01="("&table_fields(0)&"like'%"&keyword&"%'"

forj=0toubound(table_fields)

str01=str01&"or"&table_fields(j)&"like'%"&keyword&"%'"

next

str01=str01&")"

else

response.Write("<script>alert('参数错误(不能为空)!')</script>")

response.End()

endif

'全角--》半角空格

keyword=replace(keyword,"","")

'whileInStr(keyword,"")>0

'keyword=replace(keyword,"","")

'wend

keywords=split(keyword,"")

ifubound(keywords)>0then

fori=0toubound(keywords)

str02=str02&"("&table_fields(0)&"like'%"&keywords(i)&"%'"

forj=1toubound(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

endif

endfunction

'==================================================

'函数名:keyword_tag

'作用:将字符串里的关键词标记为红色

'参数:str------字符串

'keyword------标记关键词(之间用空格分开)

'返回:字符串(html格式)

'==================================================

functionkeyword_tag(byvalstr,byvalkeyword)

dimkeywords,str01,str02,i

'全角--》半角空格

keyword=replace(keyword,"","")

'whileInStr(keyword,"")>0

'keyword=replace(keyword,"","")

'wend

str01=replace(str,keyword,"<fontcolor=""#ff0000"">"&keyword&"</font>")

keywords=split(keyword,"")

ifubound(keywords)>0then

str02=str

fori=0toubound(keywords)

str02=replace(str02,keywords(i),"<fontcolor=""#ff0000"">"&keywords(i)&"</font>")

next

keyword_tag=str02

else

keyword_tag=str01

endif

endfunction

dimy

%><tralign="center">

<tdcolspan="4"><%=RecordCount%>个<fontcolor="#FF0000"><%=page%></font>/<%=pageCount%>页

<%=n%>个/页转到:

<SELECTname="select"onchange=window.open(this.options[selectedIndex].value,'_self')>

<%y=1

dowhiley<pagecount+1%>

<OPTIONvalue=?page=<%=y%>&keyword=<%=server.URLEncode(keyword)%><%ifcstr(page)=cstr(y)thenresponse.Write("selected")endif%>>第<%=y%>页</OPTION>

<%y=y+1

loop%>

</SELECT></td>

</tr>

</table>

【多字段模糊搜索的函数】相关文章:

检测函数 asp class

asp 中英文字符长度检测判断函数

Object对象的一些的隐藏函数介绍

ASP实例学习:随机生成文件名的函数

asp.net计算每个页面执行时间的方法

高手推荐的比较有用的ASP函数集合

一个非常简单的将半角转换为中文的函数

ASP常用的函数

ASP中的EVAL函数使用教程

asp 删除图片与文件函数

精品推荐
分类导航