功能:拆分字符的高级SQL查询以及分页输出 [转自:711网络工作室 http://www.tc711.com]
〈FORM action="search.asp" method=POST〉
关键字:〈INPUT name=keyword size=28 value=""〉
〈/FORM〉 [转自:711网络工作室 http://www.tc711.com]
〈%
const MaxPerPage=17 '每页显示信息数目
dim i
dim conn
dim totalPut
dim CurrentPage
dim TotalPages
Dim keyword,RS,StrSQL
//获取当前页
if not isempty(request("page")) then
currentPage=cint(request("page"))
else
currentPage=1
end if
keyword = Trim(Request("keyword")) '去掉首尾的空格
keyword = replace(keyword," ","|") '为了避免search.asp不能正确传递带有空格的字符串keyword,这里用“|”来代替空格,这是实现分页的关键。
//连接数据库
set conn=server.createobject("ADODB.Connection")
filepath=server.mappath("database.mdb")
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & filepath
Set RS=Server.CreateObject("ADODB.RecordSet")
names=Split(keyword,"|") '分割字符串
//生成SQL语句
StrSQL="Select * from Content where ("
for each name in names
StrSQL=StrSQL&"title like '%"&names(i)&"%' or "
i=i+1
next
StrSQL=left(StrSQL,len(StrSQL)-4) &") order by data desc"
//执行SQL语句
RS.Open StrSQL,conn,1,1
if rs.eof and rs.bof then
response.write"〈SCRIPT language=javascript〉alert('对不起,没有您要查询的信息!');"
response.write"javascript:history.go(-1)〈/SCRIPT〉"
else
totalPut=rs.recordcount
if currentpage〈1 then
currentpage=1
end if
if (currentpage-1)*MaxPerPage〉totalput then
if (totalPut mod MaxPerPage)=0 then
currentpage= totalPut \ MaxPerPage
else
currentpage= totalPut \ MaxPerPage + 1
end if
end if
if currentPage=1 then
showContent
else
if (currentPage-1)*MaxPerPage〈totalPut then
rs.move (currentPage-1)*MaxPerPage
dim bookmark
bookmark=rs.bookmark
showContent
else
currentPage=1
showContent
end if
end if
rs.close
end if
set rs=nothing
%〉
〈table border=0 cellspacing=0 cellpadding=0〉
〈%do while not rs.eof%〉
〈TR〉
〈TD height=20 width="445"〉〈a href="javascript:view('news.asp?ID=〈%=rs("id")%〉')"〉〈%=rs("title")%〉〈/a〉〈/TD〉
〈TD align=right width="110"〉
〈p align="left"〉[〈%=year(rs("data"))%〉-〈%=month(rs("data"))%〉-〈%=day(rs("data"))%〉]〈/TD〉〈/TR〉
〈% i=i+1
if i〉=MaxPerPage then exit do
rs.movenext
loop
%〉
〈/table〉
〈%
dim n
if totalnumber mod maxperpage=0 then
n= totalnumber \ maxperpage
else
n= totalnumber \ maxperpage+1
end if
if CurrentPage〈2 then
%〉
〈table border=0 cellspacing=0 cellpadding=0〉
〈tr〉
〈td align=left〉
【最前页】【上一页】
〈%
else
%〉
〈table border=0 cellspacing=0 cellpadding=0〉
〈tr height=30〉
〈td align=left〉
【〈a href=search.asp?page=1&key=〈%=key%〉〉最前页〈/a〉】
【〈a href=search.asp?page=〈%=(CurrentPage-1)%〉&key=〈%=key%〉〉上一页〈/a〉】
〈%
end if
if n-currentpage〈1 then
%〉
【】【最后页】
〈%
else
%〉
【〈a href=search.asp?page=〈%=(CurrentPage+1)%〉&key=〈%=key%〉〉〈/a〉】
【〈a href=search.asp?page=〈%=n%〉&key=〈%=key%〉〉最后页〈/a〉】
〈%
end if
%〉
〈/td〉
〈/tr〉
〈/table〉