您的位置:首页 > 数据库

功能完整的分页程序 + 高效SQL查询语句

2006-08-29 09:08 501 查看
主  题:【绝对原创】功能完整的分页程序 + 高效SQL查询语句
作  者:sunshift (http://www.56390.com/)
等  级:
信 誉 值:99
所属论坛:Web 开发 ASP
问题点数:10
回复次数:28
发表时间:2006-5-25 10:30:53

<%
''''''''''''''''''''''''''''''''''''
'' 功能完整的分页程序 + 高效SQL查询语句
'' Power by Love_Computer
'' love_computer@163.com
'' www.56390.com
'' 最后修改时间: 2005-05-26 12:28
'' 使用过程中有任何问题请跟贴,及时答复。
''''''''''''''''''''''''''''''''''''
TableName = "NewsInfo"
FieldName = " * "
SearchWhere = ""
OrderFieldName = "id"
OnlyFieldName = "id"
OrderType = 1
PageSizeNum = 10
Page = Trim(Request.QueryString("page"))
Call ShowTotalPage()
rsTotal = Session("rsTotal")
TotalPages = Session("TotalPages")
CurrentPage = Session("CurrentPage")
Session("rsTotal") = ""
Session("TotalPages") = ""
Session("CurrentPage") = ""
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
PageSQL = GetPageSQL(TableName,FieldName,SearchWhere,OrderFieldName,OnlyFieldName,OrderType,PageSizeNum,CurrentPage)

'''显示分页
Call ShowPages(1,1,1)

'''''创建时间:2005-6-8 15:25 [love_computer@163.com]
''''' www.56390.com
'''''最后修改时间 14:21 2005-8-30
''''获取总页数,总记录数
Sub ShowTotalPage()
Rem 以下内容直接COPY调用,不用任何修改
If Page = "" or IsEmpty(Page) = True or Page = "0" or Page = Empty Then Page = 1
StrWhere = ""
If SearchWhere = "" Then
StrWhere = ""
Else
StrWhere = " WHERE " & SearchWhere
End If
StrTotal = "SELECT COUNT([" & OnlyFieldName & "]) AS [rsTotal] FROM [" & TableName & "] " & StrWhere
Set rsTotalRecord = Conn.Execute(StrTotal)
rsTotal = rsTotalRecord("rsTotal")
rsTotalRecord.Close:Set rsTotalRecord = Nothing
CurrentPage = Page
CurrentPage = Int(CurrentPage)
If rsTotal mod PageSizeNum = 0 Then
TotalPages = int(rsTotal / PageSizeNum)
Else
TotalPages = int(rsTotal / PageSizeNum) + 1
End If
If CurrentPage > TotalPages Then CurrentPage = TotalPages
Session("rsTotal") = rsTotal'总记录
Session("TotalPages") = TotalPages'总页数
Session("CurrentPage") = CurrentPage'当前页
End Sub

'''''''''''''显示分页信息
'' www.56390.com
Sub ShowPages(ShowListOrder,ShowSelect,ShowHidden)
If rsTotal <= PageSizeNum Then Exit Sub
StrURL = PageURL
If CurrentPage = 1 Then
PreviousPage = "首页 上页"
Else
PreviousPage = "<a href='" & StrURL & "1'>首页</a> <a href='" & StrURL & CurrentPage - 1 & "'>上页</a>"
End If
If CurrentPage = TotalPages Then
NextPage = "下页 末页"
Else
NextPage = "<a href='" & StrURL & CurrentPage + 1 & "'>下页</a> <a href='" & StrURL & TotalPages & "'>末页</a>"
End If
ShowPageNum = ""
If ShowListOrder = 0 Then
If CurrentPage > 5 Then
xx = CurrentPage - 4
yy = CurrentPage + 5
Else
xx = 1
yy = 10
End If

For ii = xx TO yy
If ii > TotalPages Then Exit For
ShowPageNum = ShowPageNum & " <a href='" & StrURL & ii & "'>["
If ii = CurrentPage Then ShowPageNum = ShowPageNum & "<font color='#FF0000'><b>"
ShowPageNum = ShowPageNum & ii
If ii = CurrentPage Then ShowPageNum = ShowPageNum & "</b></font>"
ShowPageNum = ShowPageNum & "]</a>"
Next
End If

ShowTotal = "共<font color='#FF0000'><b>" & rsTotal & "</b></font>条记录<font color='#FF0000'><b>"
If ShowListOrder = 1 Then
ShowTotal = ShowTotal & CurrentPage & "/"
End If
ShowTotal = ShowTotal & TotalPages & "</b></font>页"
ShowTable = "<table width='100%' border='0' cellspacing='0' cellpadding='0'>"
If ShowSelect = 0 Then
ShowTable = ShowTable & "<form name='page' method='get' action=''>"
End If
ShowTable = ShowTable & "<tr><td align='center' valign='middle'>"
ShowTable = ShowTable & PreviousPage & " " & ShowPageNum & " " & NextPage & " " & ShowTotal & " "
Response.Write ShowTable
If ShowHidden = 0 Then
Call ShowHiddenText()
End If
ShowTable = ""
If ShowSelect = 0 Then
ShowTable = ShowTable & "转到<select name='page' onchange='this.form.submit()'>"
For n = 1 TO TotalPages
ShowTable = ShowTable & "<option value='" & n & "'"
If n = CurrentPage Then ShowTable = ShowTable & " selected"
ShowTable = ShowTable & ">" & n & "</option>"
Next
ShowTable = ShowTable & "</select>页"
End If
ShowTable = ShowTable & "</td></tr>"
If ShowSelect = 0 Then
ShowTable = ShowTable & "</form>"
End If
ShowTable = ShowTable & "</table>"
Response.Write ShowTable
End Sub

'''''''获取完整URL
'' 来源于网上 好像是阿信的吧 具体不太清楚啦 HEHE
Function FullURL()
Dim strTemp
If LCase(Request.ServerVariables("HTTPS")) = "off" Then
strTemp = "http://"
Else
strTemp = "https://"
End If
strTemp = strTemp & Request.ServerVariables("SERVER_NAME")
If Request.ServerVariables("SERVER_PORT") <> 80 Then strTemp = strTemp & ":" & Request.ServerVariables("SERVER_PORT")
strTemp = strTemp & Request.ServerVariables("URL")
If Trim(Request.QueryString) <> "" Then strTemp = strTemp & "?" & Trim(Request.QueryString)
FullURL = strTemp
End Function

''''''''''获取URL - 分页专用
'''''''''' Power by Love_Computer...
'''''''''' www.56390.com
Function PageURL()
URL = FullURL
TempUrl = Split(URL,"?")
If ubound(TempUrl) = 0 then
URL = URL & "?page="
Else
TempUrl = Split(URL,"?page=")
If ubound(TempUrl) = 0 Then
TempUrl = Split(URL,"&page")
URL = TempUrl(0) & "&page="
Else
URL = TempUrl(0) & "?page="
End If
End If
PageURL = URL
End Function

''''''''''''分页SQL语句函数
Function GetPageSQL(TableName,FieldName,SearchWhere,OrderFieldName,OnlyFieldName,OrderType,PageSizeNum,Page)
'Power by Love_Computer 2005-05-26 12:28
' www.56390.com
StrWhere = ""
StrOrder = ""
StrSQL = ""
MaxMinID = ""
If SearchWhere = "" Then
StrWhere = ""
MaxMinID = " WHERE ( [" & OnlyFieldName & "] "
Else
StrWhere = " WHERE " & SearchWhere & " "
MaxMinID = " AND ( [" & OnlyFieldName & "] "
End If
If OrderType = 0 Then
MaxMinID = MaxMinID & " > ( SELECT MAX"
StrOrder = " ORDER BY [" & OrderFieldName & "] ASC "
Else
MaxMinID = MaxMinID & " < ( SELECT MIN"
StrOrder = " ORDER BY [" & OrderFieldName & "] DESC "
End If
If Page = 1 or Page = 0 Then
StrSQL = "SELECT TOP " & PageSizeNum & " " & FieldName & " FROM [" & TableName & "] " & StrWhere & StrOrder
Else
StrSQL = "SELECT TOP " & PageSizeNum & " " & FieldName & " FROM [" & TableName & "] " & StrWhere & MaxMinID
StrSQL = StrSQL & " ([" & OnlyFieldName & "]) FROM ( SELECT TOP "
StrSQL = StrSQL & PageSizeNum * (Page - 1) & " [" & OnlyFieldName & "] FROM [" & TableName & "] " & StrWhere & StrOrder
StrSQL = StrSQL & " ) AS TempTable ) ) " & StrOrder
End If
GetPageSQL = StrSQL
End Function
%>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息