分页程序
'
定义分页全局变量
dim code,mdbsql,Page_size,Totalrec,TotalPage,currentPage
' 参数处理,并把相应代码插入code(分页)和mdbsql(数据库查询)中
Sub s_keyword(s_value)
keyword = s_value ' 建议先对keyword进行处理
if keyword <> "" then
code = code & " &keyword= " & keyword
text = Split (keyword) ' 分词
for each word in text
mdbsql = mdbsql & " and charindex(' " & trim (word) & " ',hw_area)>0 "
next
end if
end Sub
' sql搜索主程序,页次,查找字段,关键字段(唯一索引),排序字段
Sub fSql(s_page,s_table,s_str,s_key,s_order,mdbconn)
if s_page <> "" and IsNumeric (s_page) then currentPage = cint (s_page) else currentPage = 1
if mdbsql = "" then mdbsql = " 1=1 "
Totalrec = mdbconn.execute( " select count(*) from [ " & s_table & " ] where " & mdbsql)( 0 )
if Totalrec > 0 then
TotalPage = Totalrec \ Page_size
If (Totalrec Mod Page_size) Then TotalPage = TotalPage + 1
If TotalPage < 1 Then TotalPage = 1
If currentPage > TotalPage Then currentPage = TotalPage
If TotalPage = 1 then Page_size = Totalrec
if currentPage = 1 then
mdbsql = " select top " & Page_size & " " & s_str & " from [ " & s_table & " ] where " & mdbsql & " order by " & s_order & " desc "
else if s_order <> "" and s_order <> s_key then
mdbsql = " select top " & Page_size & " " & s_str & " from [ " & s_table & " ] where " & mdbsql & " and " & s_key & " not in (select top " & (currentPage - 1 ) * Page_size & " " & s_key & " from [ " & s_table & " ] where " & mdbsql & " order by " & s_order & " desc) order by " & s_order & " desc "
else
mdbsql = " select top " & Page_size & " " & s_str & " from [ " & s_table & " ] where " & mdbsql & " and " & s_key & " < (select min ( " & s_key & " ) from (select top " & (currentPage - 1 ) * Page_size & " info_id from [ " & s_table & " ] where " & mdbsql & " order by " & s_key & " desc) as T) order by " & s_key & " desc "
end if
end if
end Sub
' 分页显示
Function show_page()
temp_page = ""
If currentPage <> 1 Then temp_page = temp_page & " <a href='?page=1 " & code & " ' title='第一页'><<-</a> " Else temp_page = temp_page & " <<- "
temp_page = temp_page & " "
If currentPage > 1 Then temp_page = temp_page & " <a href='?page= " & currentPage - 1 & code & " &' title='前一页'><-</a> " Else temp_page = temp_page & " <- "
temp_page = temp_page & " "
PageStart = currentPage - 5
If PageStart < 1 Then PageStart = 1
PageEnd = PageStart + 10
If PageEnd > TotalPage Then PageEnd = TotalPage
For Temp = PageStart To PageEnd
If Temp = currentPage Then temp_page = temp_page & " [ " & Temp & " ] " Else temp_page = temp_page & " <a href='?page= " & Temp & "" & code & " ' title='第 " & Temp & " 页'> " & Temp & " </a> "
Next
If currentPage < TotalPage Then temp_page = temp_page & " <a href='?page= " & currentPage + 1 & "" & code & " ' title='下一页'>-></a> " Else temp_page = temp_page & " -> "
temp_page = temp_page & " "
If currentPage < TotalPage Then temp_page = temp_page & " <a href='?page= " & TotalPage & code & " ' title='最后一页'>->></a> " Else temp_page = temp_page & " ->> "
temp_page = temp_page & " 共 " & Totalrec & " 条记录,当前第 " & currentPage & " 页,共 " & TotalPage & " 页 "
temp_page = temp_page & " 到第<input name='gopage' id='gopage' type='text' size='3' maxlength='6' value=' " & currentPage & " ' />页 <input type='button' value='go' onclick=""location.href='?page='+gopage.value+' " & code & " '"" /> "
show_page = temp_page & ""
end Function
' 以上程序放到一个function中
dim code,mdbsql,Page_size,Totalrec,TotalPage,currentPage
' 参数处理,并把相应代码插入code(分页)和mdbsql(数据库查询)中
Sub s_keyword(s_value)
keyword = s_value ' 建议先对keyword进行处理
if keyword <> "" then
code = code & " &keyword= " & keyword
text = Split (keyword) ' 分词
for each word in text
mdbsql = mdbsql & " and charindex(' " & trim (word) & " ',hw_area)>0 "
next
end if
end Sub
' sql搜索主程序,页次,查找字段,关键字段(唯一索引),排序字段
Sub fSql(s_page,s_table,s_str,s_key,s_order,mdbconn)
if s_page <> "" and IsNumeric (s_page) then currentPage = cint (s_page) else currentPage = 1
if mdbsql = "" then mdbsql = " 1=1 "
Totalrec = mdbconn.execute( " select count(*) from [ " & s_table & " ] where " & mdbsql)( 0 )
if Totalrec > 0 then
TotalPage = Totalrec \ Page_size
If (Totalrec Mod Page_size) Then TotalPage = TotalPage + 1
If TotalPage < 1 Then TotalPage = 1
If currentPage > TotalPage Then currentPage = TotalPage
If TotalPage = 1 then Page_size = Totalrec
if currentPage = 1 then
mdbsql = " select top " & Page_size & " " & s_str & " from [ " & s_table & " ] where " & mdbsql & " order by " & s_order & " desc "
else if s_order <> "" and s_order <> s_key then
mdbsql = " select top " & Page_size & " " & s_str & " from [ " & s_table & " ] where " & mdbsql & " and " & s_key & " not in (select top " & (currentPage - 1 ) * Page_size & " " & s_key & " from [ " & s_table & " ] where " & mdbsql & " order by " & s_order & " desc) order by " & s_order & " desc "
else
mdbsql = " select top " & Page_size & " " & s_str & " from [ " & s_table & " ] where " & mdbsql & " and " & s_key & " < (select min ( " & s_key & " ) from (select top " & (currentPage - 1 ) * Page_size & " info_id from [ " & s_table & " ] where " & mdbsql & " order by " & s_key & " desc) as T) order by " & s_key & " desc "
end if
end if
end Sub
' 分页显示
Function show_page()
temp_page = ""
If currentPage <> 1 Then temp_page = temp_page & " <a href='?page=1 " & code & " ' title='第一页'><<-</a> " Else temp_page = temp_page & " <<- "
temp_page = temp_page & " "
If currentPage > 1 Then temp_page = temp_page & " <a href='?page= " & currentPage - 1 & code & " &' title='前一页'><-</a> " Else temp_page = temp_page & " <- "
temp_page = temp_page & " "
PageStart = currentPage - 5
If PageStart < 1 Then PageStart = 1
PageEnd = PageStart + 10
If PageEnd > TotalPage Then PageEnd = TotalPage
For Temp = PageStart To PageEnd
If Temp = currentPage Then temp_page = temp_page & " [ " & Temp & " ] " Else temp_page = temp_page & " <a href='?page= " & Temp & "" & code & " ' title='第 " & Temp & " 页'> " & Temp & " </a> "
Next
If currentPage < TotalPage Then temp_page = temp_page & " <a href='?page= " & currentPage + 1 & "" & code & " ' title='下一页'>-></a> " Else temp_page = temp_page & " -> "
temp_page = temp_page & " "
If currentPage < TotalPage Then temp_page = temp_page & " <a href='?page= " & TotalPage & code & " ' title='最后一页'>->></a> " Else temp_page = temp_page & " ->> "
temp_page = temp_page & " 共 " & Totalrec & " 条记录,当前第 " & currentPage & " 页,共 " & TotalPage & " 页 "
temp_page = temp_page & " 到第<input name='gopage' id='gopage' type='text' size='3' maxlength='6' value=' " & currentPage & " ' />页 <input type='button' value='go' onclick=""location.href='?page='+gopage.value+' " & code & " '"" /> "
show_page = temp_page & ""
end Function
' 以上程序放到一个function中
使用前定义好变量
'
定义参数全局变量
dim keyword
Page_size = 20
mdbsql = " flag = 1 " ' 可以先定义固定的搜索条件
' 函数会根据参数自动调整code和mdbsql
Call s_keyword( trim (Request( " keyword " )))
Call s_sql( trim (Request( " page " )), " title " , " id " , " id " )
' 以上代码一般放在程序开头
dim keyword
Page_size = 20
mdbsql = " flag = 1 " ' 可以先定义固定的搜索条件
' 函数会根据参数自动调整code和mdbsql
Call s_keyword( trim (Request( " keyword " )))
Call s_sql( trim (Request( " page " )), " title " , " id " , " id " )
' 以上代码一般放在程序开头
使用
'
内容显示
if Totalrec > 0 then
set rs = server.createobject( " adodb.recordset " )
rs.open mdbsql,toconn, 1 , 1
If rs.bof and rs.Eof Then
response.Write( " 暂时没有信息 " )
else
For Temp_FN = 1 to Page_size
If rs.Eof Then Exit For
response.write rs( " title " )
rs.movenext
next
response.write show_page() ' 分页显示
end if
else
response.Write( " 暂时没有信息 " )
end if
if Totalrec > 0 then
set rs = server.createobject( " adodb.recordset " )
rs.open mdbsql,toconn, 1 , 1
If rs.bof and rs.Eof Then
response.Write( " 暂时没有信息 " )
else
For Temp_FN = 1 to Page_size
If rs.Eof Then Exit For
response.write rs( " title " )
rs.movenext
next
response.write show_page() ' 分页显示
end if
else
response.Write( " 暂时没有信息 " )
end if
本文转自博客园cloudgamer的博客,原文链接:asp分页系统,如需转载请自行联系原博主。