多字段模糊搜索的函数

前段时间写了一个多字段模糊搜索的函数(类似baidui)。函数代码如下
'==================================================
'函数名:keyword_sousuo
'作 用:生成sql查询条件
'参 数:table_field ------ 表的字段名(之间用逗号分开)
' keyword ------ 搜索关键词(之间用空格分开)
'返 回:sql查询条件
'==================================================
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('参数错误(不能为空)!')</script>")
   response.End()
  end if

'全角--》半角 空格
 '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
'==================================================
'函数名:keyword_tag
'作 用:将字符串里的关键词标记为红色
'参 数:str ------ 字符串
' keyword ------ 标记关键词(之间用空格分开)
'返 回:字符串(html格式)
'==================================================
function keyword_tag(byval str,byval keyword)
 dim keywords,str01,str02,i
   '全角--》半角 空格
 '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
以下是一个例子 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")+";DefaultDir=;DRIVER={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 while 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="?"><tr><td align="center"><input name="keyword" type="text" id="keyword" value="<%=keyword%>" size="30" />
   <input type="submit" name="Submit" value="搜索" />
 <span class="STYLE3">(关键词之间用空格分开) </span></td>

</tr></form>
 <tr>
 <td height="5"></td> 
</table>

<table width="600" border="1" align="center" cellpadding="4">
 <tr align="center"> 
 <td width="131">标题</td>
 <td width="131">动作</td>
 <td width="131">用户名</td>
 <td width="132">时间</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%>
 <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

'==================================================
'函数名:keyword_sousuo
'作 用:生成sql查询条件
'参 数:table_field ------ 表的字段名(之间用逗号分开)
' keyword ------ 搜索关键词(之间用空格分开)
'返 回:sql查询条件
'==================================================
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('参数错误(不能为空)!')</script>")
   response.End()
  end if

'全角--》半角 空格
 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
'==================================================
'函数名:keyword_tag
'作 用:将字符串里的关键词标记为红色
'参 数:str ------ 字符串
' keyword ------ 标记关键词(之间用空格分开)
'返 回:字符串(html格式)
'==================================================
function keyword_tag(byval str,byval keyword)
 dim keywords,str01,str02,i
   '全角--》半角 空格
 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"> 
 <td colspan="4"> <%=RecordCount%>个  <font color="#FF0000"><%=page%></font>/<%=pageCount%>页 
  <%=n%>个/页 转到: 
 <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%>页</OPTION>
 <%y=y+1 
 loop%>
 </SELECT></td>
 </tr>
</table>

(0)

相关推荐

  • jquery ztree实现模糊搜索功能

    本文分享了jquery ztree实现模糊搜索功能两个实例,供大家参考,具体内容如下 ztree官方demo代码里的: 根据参数查找节点 以上文件修改成如下代码 <!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - getNodeByParam / getNodesByParam / getNodesByParamFuzzy</TITLE> <meta http-equiv="con

  • 用java等语言仿360首页拼音输入全模糊搜索和自动换肤

    360首页搜索效果如下 1.完成编写的schoolnet校园网主要目录结构如下 主要实现支持中文.拼音首字母.拼音全字母的智能搜索和换肤 页面效果如下 主要核心代码如下 1.head.jsp <%@page import="java.io.File"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@

  • jquery zTree异步加载、模糊搜索简单实例分享

    本文实例为大家讲解了jquery zTree树插件的基本使用方法,具体内容如下 一.节点模糊搜索功能:搜索成功后,自动高亮显示并定位.展开搜索到的节点. 二.节点异步加载:1.点击展开时加载数据:2.选中节点时加载数据. 前台代码如下: <script type="text/javascript"> //ztree设置 var setting = { view: { fontCss: getFontCss }, check: { enable: true }, data:

  • c#反射表达式树模糊搜索示例

    复制代码 代码如下: public static Expression<Func<T, bool>> GetSearchExpression<T>(string SearchString)        {            Expression<Func<T, bool>> filter = null; if (string.IsNullOrEmpty(SearchString)) return null;            var l

  • mysql 模糊搜索的方法介绍

    网上查了一下,有两种方法,第一种最土的方法:使用like语句第二种听涛哥说用全文索引,就在网上搜一下: 如何在MySQL中获得更好的全文搜索结果 很多互联网应用程序都提供了全文搜索功能,用户可以使用一个词或者词语片断作为查询项目来定位匹配的记录.在后台,这些程序使用在一个SELECT 查询中的LIKE语句来执行这种查询,尽管这种方法可行,但对于全文查找而言,这是一种效率极端低下的方法,尤其在处理大量数据的时候. mysql针对这一问题提供了一种基于内建的全文查找方式的解决方案.在此,开发者只需要

  • 模拟一个类似百度google的模糊搜索下拉列表

    复制代码 代码如下: // JavaScript Document function onChangehoverLi(thisLi){ $("#searchtext").val($(thisLi).html()); $("#suggest_ul").hide(0); validateform2(); } $(function(){ //载入时隐藏下拉li $("#suggest_ul").hide(0); }); //Ajax 动态获取关键字 /

  • 多字段模糊搜索的函数

    前段时间写了一个多字段模糊搜索的函数(类似baidui).函数代码如下 '================================================== '函数名:keyword_sousuo '作 用:生成sql查询条件 '参 数:table_field ------ 表的字段名(之间用逗号分开) ' keyword ------ 搜索关键词(之间用空格分开) '返 回:sql查询条件 '==========================================

  • asp 多字段模糊搜索的函数

    比较简单直接的sql语句 Recordset1.Source = "SELECT * FROM 表 WHERE 字段 LIKE '%" + Replace(Recordset1__MMColParam, "'", "''") + "%' or 字段2 like '%" + Replace(Recordset1__MMColParam, "'", "''") + "%' ORDE

  • WordPress中编写自定义存储字段的相关PHP函数解析

    WordPress 的自定义字段就是文章的 meta 信息(元信息),利用这个功能,可以扩展文章的功能,是学习 WordPress 插件开发和主题深度开发的必备知识,方便给文章储存一些额外的自定义内容. 自定义字段不光给插件开发者使用,WordPress 的特色图像.自定义页面模板等功能的信息都储存在自定义字段的表单里边. 在文章编辑器界面的 "自定义栏目" 板块可以管理除了名字开头是 "_" 的字段(没有 "自定义栏目" 板块请在右上角的 &q

  • Lua中函数与面向对象编程的基础知识整理

    函数 1. 基础知识 调用函数都需要写圆括号,即使没有参数,但有一种特殊例外:函数若只有一个参数且参数是字面字符串或table构造式,则圆括号可有可无,如dofile 'a.lua',f{x=10, y=20}. Lua为面向对象式的调用提供冒号操作符的特殊语法,如o.foo(o, x)等价于o:foo(x).和Javascript类似,调用函数时提供的实参数量可以与形参数量不同,若实参多了则舍弃,不足则多余的形参初始化为nil. 1.1 多重返回值 Lua允许函数返回多个结果,函数返回如ret

  • SQLServer性能优化--间接实现函数索引或者Hash索引

    SQLServer中没有函数索引,在某些场景下查询的时候要根据字段的某一部分做查询或者经过某种计算之后做查询,如果使用函数或者其他方式作用在字段上之后,就会限制到索引的使用,不过我们可以间接地实现类似于函数索引的功能. 另外一个就是如果查询字段较大或者字段较多的时候,所建立的索引就显得有点笨重,效率也不高,就需要考虑使用一个较小的"替代性"字段做等价替换,类似于Hash索引, 本文粗浅地介绍两种上述两种问题的解决方式,仅供参考. 1,在计算列上建索引,实现"函数索引"

  • Lua教程(二十):Lua调用C函数

    Lua可以调用C函数的能力将极大的提高Lua的可扩展性和可用性.对于有些和操作系统相关的功能,或者是对效率要求较高的模块,我们完全可以通过C函数来实现,之后再通过Lua调用指定的C函数.对于那些可被Lua调用的C函数而言,其接口必须遵循Lua要求的形式,即typedef int (*lua_CFunction)(lua_State* L).简单说明一下,该函数类型仅仅包含一个表示Lua环境的指针作为其唯一的参数,实现者可以通过该指针进一步获取Lua代码中实际传入的参数.返回值是整型,表示该C函数

  • php常用ODBC函数集(详细)

    ODBC连接类函数odbc_connect函数:打开一个ODBC连接odbc_close函数:关闭一个已经打开的ODBC连接odbc_close_all函数:关闭所有已经打开的ODBC连接odbc_pconnect函数:打开一个持续有效的ODBC连接 ODBC操作类函数odbc_commit函数:更新所有处于未决状态的操作odbc_do函数:在打开的ODBC连接上执行SQL语句odbc_exec函数:执行SQL语句odbc_execute函数:执行一个预置的SQL语句odbc_free_resu

  • PHP5函数小全(分享)

    很多PHP前辈都写了PHP大全,但是我看了发现那些所谓的"大全"根本就不全,甚至比我整理的这个列表内的函数还要少,竟然冠名"大全",让我不愤,背道而驰,整个"小犬". usleep() 函数延迟代码执行若干微秒.unpack() 函数从二进制字符串对数据进行解包.uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID.time_sleep_until() 函数延迟代码执行直到指定的时间.time_nanosleep() 函数延迟代码执

  • WordPress开发中的get_post_custom()函数使用解析

    同get_post_meta()一样,用于返回文章的自定义字段值得一个函数,只不过get_post_custom()函数使用起来更简单,如果在循环中使用你甚至不需要设置任何参数. 其实get_post_custom()函数的基础实现与get_post_meta()大同小异~ get_post_custom()使用 get_post_custom($postid); 只接受一个参数 $postid文章id: 实例演示 if (have_posts()) : while (have_posts())

  • django 中的聚合函数,分组函数,F 查询,Q查询

    先以mysql的语句,聚合用在分组里, 对mysql中groupby 是分组 每什么的时候就要分组,如 每个小组,就按小组分, group by 字段 having 聚合函数 #举例 :求班里的平均成绩, select Avg(score) from stu 在django中 聚合 是aggreate(*args,**kwargs),通过QuerySet 进行计算.做求值运算的时候使用 分组 是annotate(*args,**kwargs),括号里是条件,遇到 每什么的时候就要分组, 先从mo

随机推荐