一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp站内搜索源码

时间:2010-10-14 编辑:简简单单 来源:一聚教程网



asp站内搜索源码




请输入关键字:





多个关键字之间用"空格"或"逗号"隔开!



<%
if request.form("submit")<>"" then
 dim database,conn,connstr
 database="example.mdb"   '数据库名称
 set conn=server.createobject("adodb.connection")   '创建connection对象
 'provider参数设置数据库的驱动程序,access使用ole db驱动程序;data source参数设置数据库的实际路径及文件名称
 connstr="provider=microsoft.jet.oledb.4.0;data source=" & server.mappath(database)
 conn.open connstr   '采用open方法连接数据库
 keywords=request.form("key")   '关键字
 if trim(keywords)="" then
  response.write ""
  response.end
 end if
 keywords=replace(keywords,","," ")   '将获得的字符串中的英文逗号换成空格
 keywords=replace(keywords,","," ")   '将获得的字符串中的中文逗号换成空格
 keywords=trim(keywords)   '去掉字符串左边和右边空格
 sql=""   '关键字匹配sql语句
 keywordarray=split(keywords)   '将输入的字符串用空格分开,获得一个数组
 max=ubound(keywordarray)   '得到这个数组的最大下标值,即输入的关键字个数
 if max=0 then   '说明只输入了一个关键字,那么就不需要循环处理
  sql=sql&"content like '%"&keywordarray(0)&"%'"   '网站关键字模糊搜索
 else   '如果含有多个关键字,采用循环处理sql语句
  for i=0 to max   '如果关键字很多,我们要求每一个搜索都要匹配每一个关键字,通过循环来实现
   if i=0 then   '写入下面sql语句作为开头
    sql=sql&"(content like '%"&keywordarray(i)&"%' and "
   elseif i=max then   '如果循环到最后一个关键字,写入下面sql语句作为结尾
    sql=sql&"content like '%"&keywordarray(i)&"%')"
   else   '如果关键字不是开头也不是结尾,写入下面的sql语句
    sql=sql&"content like '%"&keywordarray(i)&"%' and "
   end if
  next
 end if
 set rs=server.createobject("adodb.recordset")   '创建recordset对象
 '从数据库中查询content字段中包含关键字的记录
 sql="select * from example where "&sql&""
 rs.open sql,conn,1,3   '执行查询,结果保存在rs中
 if rs.eof or rs.bof then   '不存在记录
  response.write("")
  response.end
 else   '存在记录
  do while not rs.eof   '循环显示所有记录的id
   id=rs("id")
   %>
   <%=rs("id")%>  <%=rs("content")%>

   <%
   rs.movenext
  loop
 end if
end if
%>

 

热门栏目