呵呵!写了个分页函数,有兴趣可以来看看,自认为比较方便实用。

bubuy 2003-10-17 12:16:40
大家来看看,用用,顺便提提意见。
大家可以添加新得功能或修改已有得功能
不过别忘了改后给我一份噢,谢谢
bubuy@sohu.com
也可以发在帖子里
给50分给修改该程序比较好的大哥。
...全文
67 49 打赏 收藏 转发到动态 举报
写回复
用AI写文章
49 条回复
切换为时间正序
请发表友善的回复…
发表回复
bubuy 2003-10-20
  • 打赏
  • 举报
回复
to siyou(QQ~在线)
你说得也很不错,在此感谢你。

不过我这里也有几句话要说,你说得
情况给我得感觉就想是要大家写好得
SQL语句,而不是分页问题,呵呵。。。

对了,还有,如果一个服务器读上千条得
数据都会出问题得话,请你换一家得服务
器用用,呵呵,像我们这里得服务器有时
要读上几十万条得记录,依然是毫无问题
得,我们用得是SQLServer数据库。
bubuy 2003-10-20
  • 打赏
  • 举报
回复
呵呵,感谢大家,因为星期陆和星期天不上班
所以没来回复大家,大家提得意见很好,我一
定注意。
在这里特别感谢 windancer(风精灵)

我马上结帐了。

还有有些大哥说要是读上千条信息会影响效率,
其实我不占成这种说法,为什么呢?
在我们现时得网页中分页上千条得网页太少呢
我常看到得就是搜索引擎;它得分页根本就不是一回事了。
还有大家说上千条记录影响效率,我不这么认为,不知道
大家用上千条得记录试过没有,没有请试试。
mycl3344 2003-10-19
  • 打赏
  • 举报
回复
無法跳轉﹐能不能實現啊?實現了發我一份如何?
coldwindfan@163.com
nchen123 2003-10-18
  • 打赏
  • 举报
回复
用存储过程输出 xml, 配合 xsl 解析最好。
hxy2003 2003-10-18
  • 打赏
  • 举报
回复
UP
tfy_002 2003-10-18
  • 打赏
  • 举报
回复
谢谢楼主,我现在正好需要!!!!!!!!!!!!!!
收藏
  • 打赏
  • 举报
回复
好象很复杂呀.
----------------------------------------------------------------------------------
<%dim spl,curpage,i,rs,theday,fl,rs2
nclassid=trim(request("nclassid"))
set rs2=server.createobject("adodb.recordset")
sql="SELECT * from article where nclassid="&cstr(nclassid)&" ORDER BY articleid DESC"
rs2.open sql,conn,1,1%>
--------------------------------------------------------------------------------
下面是一个表格大家保存成.asp就可以了,连同上面,把你数据库中相应的字段改好了就可以了
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="2%" rowspan="3"> </td>
<td width="98%" height="16" valign="top"> </td>
</tr>
<tr>
<td><table align=center border=0 cellpadding=0 cellspacing=0
width=100%>
<tbody>
<tr>
<td height="56" valign="top">
<% page=request.QueryString(page)
if request("page")="" then
curpage = 1
else
curpage = cint(request("page"))
end if
rs2.pagesize=25
rs2.absolutepage = curpage
for i = 1 to rs2.pagesize
response.write "<li type=circle><a href='content.asp?articleid="& rs2("articleid")& "' target=_blank><font size=2>"
'if not isempty(rs("img")) then
'response.write "<font color=#FF0400 font size=1>[图文]</font>"
'end if
response.write trim(rs2("title")) & "</font></a><font color=#6365CE font size=1>("
theday=cstr(YEAR(rs2("dateandtime")))&"-"&cstr(MONTH(rs2("dateandtime")))&"-"&cstr(DAY(rs2("dateandtime")))
response.write theday & ")</font>"
rs2.movenext
if rs2.eof then
i=i+1
exit for
end if
next %>
</td>
</tr>
</tbody>
</table></td>
</tr>
<tr>
<td height="31"><div align="center" width=100%>
<div align="center" width=100%>
<div align="center" width=100%>
<%
' response.write "<hr size=0 width='98%'><div align=center>"
response.write "<font size=2 >第</font><font size=2 color=red>" + cstr(curpage) + "</font><font size=2 >页/总</font><font size=2 color=red>" + cstr(rs2.pagecount) + "</font><font size=2 >页</font> "
response.write "<font size=2 >本页</font><font size=2 color=red>" + cstr(i-1) + "</font><font size=2 >条/总</font><font size=2 color=red>" + cstr(rs2.recordcount) + "</font><font size=2 >条</font>"
if curpage = 1 then %>
<font color="#000000" size="2">首页 前页</font>
<%else
response.write "<font size=2 ><a href='xlmore.asp?nclassid=" & cstr(nclassid) & "&page=1'>首页</a></font> <font size=2 ><a href='xlmore.asp?nclassid=" & cstr(nclassid) & "&page=" & cstr(curpage-1) & "'>前页</a></font> "
end if
if curpage = rs2.pagecount then %>
<font color="#000000" size="2">后页 末页</font>
<%else
response.write "<font size=2 ><a href='xlmore.asp?nclassid=" & cstr(nclassid) & "&page=" & cstr(curpage+1) & "'>后页</a></font> <font size=2 ><a href='xlmore.asp?nclassid=" & cstr(nclassid) & "&page=" & cstr(rs2.pagecount) & "'>末页</a></font> "
end if
rs2.close
set rs2=nothing
conn.close
set conn=nothing
%>
</div>
</div>
</div></td>
</tr>
</table>
masterlijianfei 2003-10-18
  • 打赏
  • 举报
回复
提供一点小建议,假如每页10条纪录,那么第一页就显示select top(10) * from [tablename] where ...,第二页取20条出来分页select top(20) * from [tablename] where ...,如此类推select top(n*10) * from [tablename] where ...,这样的效率会高很多,就算数据十分庞大,数据库一次才取10条,20条纪录来分页,况且浏览的时候一般看3~5页基本上就解决问题了,大家评论吧,我就是这样节省服务器的资源的
liuzxit 2003-10-18
  • 打赏
  • 举报
回复
只是对ado的分页功能稍做加工而已 ,没多大意义,还不如用xml来做
ChinaE114 2003-10-18
  • 打赏
  • 举报
回复
masterlijianfei(田螺) :这样就无法获得总的参数了,如共有几条数据,分几页等
qunluo 2003-10-18
  • 打赏
  • 举报
回复
又学了一招!!!
好功夫!!!
windancer 2003-10-18
  • 打赏
  • 举报
回复
感觉在style的定义上还要多加一些属性
windancer 2003-10-18
  • 打赏
  • 举报
回复
刚才一直在写,没看到上面几位大侠的留言,申明一下:
我刚才改自楼主函数的分页类没有考虑过什么效率问题,主要从重用性上考虑,当然功能还待扩展。

如果对效率要求比较高的,还是考虑用存储过程"要多少取多少"
windancer 2003-10-18
  • 打赏
  • 举报
回复
为了支持澎湃兄,顺便要点小分:P
<%
'===========================================================
'分页类,大体思想由.Net的DataGrid的使用方式而来
'功能:自动生成datagrid列表头和内容,以及分页栏
'根据网友bubuy (澎湃 NoMoneyToBuy)得分页函数修改成类
'使用示例:
'dim DG
'dim Url
'dim Fld(2)
'dim FldName(2)
'dim FldWidth(2)
'Fld(0) = "ID"
'Fld(1) = "Title"
'Fld(2) = "Input_Date"
'FldName(0) = "编号"
'FldName(1) = "标题"
'FldName(2) = "录入日期"
'FldWidth(0) = "10%"
'FldWidth(1) = "60%"
'FldWidth(2) = "30%"
'set DG = new DataGrid
'DG.DataSource = rs_Grid
'DG.titleColor = "#DCE19D"
'DG.PageSize = 1
'DG.Fields = Fld
'DG.FieldsName = FldName
'DG.fieldWidth = FldWidth
'Url = request.ServerVariables("URL") & "?Param=testParameter"//存在原有参数的情况
'DG.Url = Url
'DG.Generate()
'=============Designed By windancer 2003.10.17===============
Class DataGrid
Private obj_RecordSet ' recordset
Private int_PageSize ' 每页纪录数
'两个数组保存数据库字段名和中文名称
Private Arr_Field ' 数据库字段
Private Arr_FieldName ' 字段显示名称()
Private Arr_FieldWidth ' 字段显示宽度
Private str_TitleColor ' 表头颜色#efffce
Private str_Url '请求的URL
Private str_Error ' 出错信息

Private Sub Class_Initialize()
int_PageSize = 10
str_TitleColor = "#ffffff"
str_Error = ""
End Sub
'===============================================================
'属性信息
'================================================================
'-----------------------------------
'数据源,暂时只支持RecordSet
'-----------------------------------
Public Property Let dataSource(obj)
set obj_RecordSet = obj
End Property

Public Property Let pageSize(intValue)
int_PageSize = intValue
End Property

Public Property Get pageSize
PageSize= int_Categoryid
End Property

Public Property Let Fields(Arr)
Arr_Field = Arr
End Property

Public Property Get Fields
Fields= Arr_Field
End Property

Public Property Let fieldsName(Arr)
Arr_FieldName = Arr
End Property

Public Property Get fieldsName
fieldsName= Arr_FieldName
End Property

Public Property Let fieldWidth(Arr)
Arr_FieldWidth = Arr
End Property

Public Property Get fieldWidth
fieldWidth= Arr_FieldWidth
End Property

Public Property Let titleColor(strValue)
str_TitleColor = strValue
End Property

Public Property Get titleColor
titleColor= str_TitleColor
End Property
'-----------------------------------------------------
'这个属性是为了保存Url路径
'如果当前路径带有参数,那么就用&Page=x,否则就用?Page=x
'------------------------------------------------------
Public Property Let Url(StrValue)
str_Url = StrValue
End Property

Public Property Get Url
Url= str_Url
End Property

'================================================================
'方法
'================================================================
'----------------------------------------------------------------
'显示当前错误
'----------------------------------------------------------------
Private Sub ShowLastError()
response.Write(str_Error)
response.End()
End Sub
'----------------------------------------------------------------
'Generate()
'利用ado分页
'-----------------------------------------------------------------
Public Sub Generate()
'----检查参数--------------------------
Check
'---------变量声明-----------------------------------
Dim FieldCount '显示字段
FieldCount = Ubound(Arr_Field) + 1
Dim CurrentPage '当前页
Dim PgCount '总页数
Dim RecCount '记录数,本来用rs.recordCount可以取到,保存下来效率会比较高
Dim HasOtherParam 'URL是否包含其他参数
Dim PageParam '当前分页Url参数
Dim PageInfomation '当前分页状态信息
Dim Seperator '设置分隔符
Seperator = "  "
'-------------处理Url参数---------------------------
if instr(str_Url,"?")>0 then
HasOtherParam = true
PageParam = "&Page="
else
HasOtherParam = false
PageParam = "?Page="
end if
'----------获取当前页--------------------------------
CurrentPage = request.QueryString("Page")
if CurrentPage="" then
CurrentPage=1
else
CurrentPage=Cint(CurrentPage)
end if
'-----------处理数据源------------------------------
obj_RecordSet.PageSize = int_PageSize
RecCount = obj_RecordSet.RecordCount
PgCount = obj_RecordSet.PageCount
IF obj_RecordSet.Eof Then
Response.Write("<center><font stlye='font-size:14px;' color='#ff0000'>对不起,没有记录!</font></center>")
Else
'-----------处理ADO分页----------------------------
IF CurrentPage < 1 Then
CurrentPage = 1
Else
If CurrentPage>PgCount Then
CurrentPage = PgCount
End If
End IF
obj_RecordSet.absolutepage = CurrentPage

Response.Write("<table width=100% border='0' cellpadding='0' cellspacing='0' style='font-size:12px;'>")
'---------------翻页链接-----------------------------
Dim FirstLink,PrevLink,NextLink,LastLink '定义向上和向下翻的变量
'-----------------------首页-------------------------
if CurrentPage>1 then
FirstLink = "<a href='" & URL & PageParam & "1'>首页</a>"
PrevLink = "<a href='" & URL & PageParam & Cstr(CurrentPage-1) & "'>上一页</a>"
else
FirstLink = "首页"
PrevLink = "上一页"
end if
'------------下一页----------------
if CurrentPage<PgCount then
NextLink = "<a href='" & URL & PageParam & Cstr(CurrentPage+1) & "'>下一页</a>"
LastLink = "<a href='" & URL & PageParam & PgCount & "'>尾页</a>"
else
NextLink = "下一页"
LastLink = "尾页"
end if
PageInfomation = FirstLink & Seperator & PrevLink & Seperator & NextLink & Seperator & LastLink & Seperator & "每页" & Cstr(int_PageSize) & "条记录" & Seperator & "共" & PgCount & "页" & Seperator & "目前第" & CurrentPage & "页" & Seperator
Response.Write("<tr><td align=center>")
Response.Write("<table width='100%' border='1' cellpadding='2' cellspacing='2' bordercolor='#999999'>")
'---------------设置表头-----------------
Response.Write("<tr bgcolor='" & str_TitleColor & "'>")
Dim i
For i=0 to FieldCount -1
Response.Write("<td align='center' width='" & Arr_FieldWidth(i) & "'><font style='font-size:14px;'><b>" & Arr_FieldName(i) & "</b></font></td>")
Next
Response.Write("</tr>")
'---------------------输出内容---------------------------------
i=0
While (not obj_RecordSet.EOF) and i<int_PageSize
Dim Cursor
Response.Write("<tr>")
For Cursor = 0 to FieldCount -1
Response.Write("<td align='center'>" & obj_RecordSet(Arr_Field(Cursor)) & "</td>")
Next
Response.Write("</tr>")
i=i+1
obj_RecordSet.MoveNext
Wend
'------------------------输出分页条------------------------------------
Response.Write("<tr><td align='right' colspan='" & Cstr(FieldCount) & "'>" & PageInfomation & "</td></tr>")
response.Write("</table></td></tr></table>")
End IF
End Sub
'----------检查参数是否正确---------------
Private Sub Check()
if Ubound(Arr_Field)<>Ubound(Arr_FieldName) then
str_Error="Fields数组和FieldName数组维数必须相同"
end if
if obj_RecordSet=empty then
str_Error="数据源不能为空,请设置dataSource属性"
end if
if int_PageSize="" then
str_Error="数据源不能为空"
end if
ShowLastError
End Sub
End Class


%>
bubuy 2003-10-17
  • 打赏
  • 举报
回复
这样使用
page=request("page")
sql="select * from [table]"
url="xx.asp"当然也可以是"xx.asp?bubuy=1"
conn大家都知道是定义的什么吧
调用就这样
Call cutPage(sql,conn,"3",page,url)'3代表每页显示3条记录
bubuy 2003-10-17
  • 打赏
  • 举报
回复
<%
'================= ProgrammName: 分页函数 =======================
' ================ Programmed by Bubuy =====================
' ==================== 2003.10.16 ========================
Function cutPage(sqlStr,Conn,dateNums,pageNums,URLs)'利用ADO分页的函数
Dim sql,Cn,dateNum,pageNum,URL,rsDate
Sql=Trim(sqlStr) '获得sql语句。
Set Cn=Conn '获得数据对象
dateNum=Cint(dateNums) '获得每页得记录数
pageNum=Cint(pageNums) '获得当前页码
URL=Trim(URLs) '获得路径
Set rsDate=Server.CreateObject("ADODB.Recordset")
rsDate.PageSize=dateNum
rsDate.Open Sql,cn,1,1
IF rsDate.Eof Then
Response.Write("<center><font stlye='font-size:14px;' color='#ff0000'>对不起,没有记录!</font></center>")
Else
IF pageNum="" or pageNum<1 Then
pageNum=1
ElseIf pageNum>rsDate.PageCount Then
pageNum=rsDate.PageCount
End IF
rsDate.absolutepage =pageNum
Dim recordHead,recordLast '定义当前页开头记录和结束记录
recordHead=1
If pageNum>1 Then recordHead=dateNum*(pageNum-1)
If pageNum>=rsDate.PageCount Then
recordLast=rsDate.RecordCount
Else
recordLast=dateNum*pageNum
End If
Response.Write("<table width=100% border='0' cellpadding='0' cellspacing='0' style='font-size:12px;'>")
Response.Write("<tr><td height=38 align=left><font stly='font-size:12px;'>(第"&recordHead&"-"&recordLast&"条,共"&rsDate.Recordcount&"条,每页显示"&dateNum&"条)</font></td></tr>")
Dim URLa '定义判断输入得URL包含?没有的变量
Dim upPage,downPage,allPage '定义向上和向下翻的变量
Dim allWrite '定义输出
upPage=pageNum-1
downPage=pageNum+1
URLa=Split(URL,"?",-1,1)
If URLa(0)=URL Then
upPage="<a href=" & URL & "?page=" & upPage &" stlye='font-size:12px;'>上一页</a> "
If pageNum=1 Then upPage=""
downPage="<a href=" & URL & "?page=" & downPage &" stlye='font-size:12px;'>下一页</a>"
If pageNum=rsDate.PageCount Then downPage=""
Else
upPage="<a href=" & URL & "&page=" & upPage &" stlye='font-size:12px;'>上一页</a> "
If pageNum=1 Then upPage=""
downPage="<a href=" & URL & "&page=" & downPage &" stlye='font-size:12px;'>下一页</a>"
If pageNum=rsDate.PageCount Then downPage=""
End If
allWrite=upPage & downPage & "  共" & rsDate.PageCount & "页 " & "目前第"& pageNum &"页"
allwrite="<font style='font-size:12px;'>" & allWrite & " 到第<input type='text' style='width:30px;'>页<input type='submit' value='GO'></font>"
Response.Write("<tr><form name='formPage' method='post' action="&URL&"><td height=30 align=right style='font-szie=12px;'>" & allWrite & "</td></form></tr>")
Response.Write("<tr><td align=center>")
Response.Write("<table width=100% border='0' cellpadding='0' cellspacing='0'>")
Response.Write("<tr>")
Response.Write("<tr bgcolor='#efffce' height=25>")
Dim id,i
For i=0 to rsDate.Fields.Count-1 '设置表头
Response.Write("<td align='center'><font style='font-size:14px;'><b>"&rsDate.Fields(i).Name&"</b></font></td>")
Next
Response.Write("</tr>")
id=0
While not rsDate.EOF and id<dateNum
id=id+1
If id Mod 2=0 then
Response.Write("<tr bgcolor=#f7f6e7>")
Else
Response.Write("<tr bgcolor=#ffffff>")
End If
For Each fils in rsDate.Fields
Response.Write("<td align='center' height=20>"&fils&"</td>")
Next
Response.Write("</tr>")
rsDate.MoveNext
Wend
Response.Write("<tr height=30 bgcolor='#efffce'>")
For i=0 to rsDate.Fields.Count-1 '设置表尾
Response.Write("<td align='center'><font style='font-size:14px;'><b>"&rsDate.Fields(i).Name&"</b></font></td>")
Next
Response.Write("</tr>")
Response.Write("</table></td></tr>")
Response.Write("<tr><form name='formPage1' method='post' action="&URL&"><td height=30 align=right>" & allWrite & "</td></form></tr>")
End IF
rsDate.close
Set rsDate=nothing
End Function

%>
CTBOK 2003-10-17
  • 打赏
  • 举报
回复
修改了一下,多了一个属性,另外,这个分页也可以用于数据查找的,数据查找的SQL语句先用COOKIE记录下来,然后在
Example.strsql = "select * from exp" 'sql语句

这里不用"select * from exp",而是用COOKIE记录下的那个语句,就能作到查找分页的原理了
CTBOK 2003-10-17
  • 打赏
  • 举报
回复
再补充一下,什么RDS等的分页都是没用的,一次把数据都读出来的话,小数据量还可以,几千上万,如果几十万,几百万的话,服务器就完蛋了,要什么,就读什么,才是可行之道
CTBOK 2003-10-17
  • 打赏
  • 举报
回复
老实说,这个分页的函数不好。
一来,函数难用,难提供相应的方法与属性
二来,分页方法不对,rsDate.absolutepage =pageNum这种分页方法只会耗费服务器的大部分资源,如果分页数上千的话,这种方法几十个就能让服务器完蛋了

我不是有心在这里捣乱,其实之前我写了一个类的,用于分页,CSDN不少网友也有了,这次我放出来。用类比用函数要好得多,而且使用方便,而且不用自己创建RS,也不用知道分页是怎样实现的,只要会用就OK了,现在我自己也是用这个,快忘了分页是怎样写的了

http://www.patentcn.com/web.rar

有例程及说明,其实分页就是这么简单
liuyear 2003-10-17
  • 打赏
  • 举报
回复
大量数据集分页效率如何?
加载更多回复(29)

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧