几个高效分页源码

几个高效分页源码。请看下面的演示链接:

http://demo.54caizi.com/PageAC/

以下是毛虫的分页代码

<%@ language = "vbscript" codepage = 936%>
<%
option explicit '强制定义变量
'==========================================================================
'毛虫的快速分页
'mail:mc@flashado.com
'主页: http://www.flashado.com
'qq:69862476
'本分页供初学者学习,技术上有不当之处,还请各位大侠修正
'==========================================================================
dim idcount'记录总数
dim pages'每页条数
dim pagec'总页数
dim page'页码
dim pagenc '每页显示的分页页码数量=pagenc*2+1
pagenc=2
dim pagenmax '每页显示的分页的最大页码
dim pagenmin '每页显示的分页的最小页码
page=clng(request("page"))
dim start'程序开始的时间
dim endt'程序结束的时间
dim datafrom'数据表名
datafrom="table1"
dim conn,rs
dim datapath '数据库路经
dim sqlid'本页需要用到的id
dim myself'本页地址
myself = request.servervariables("path_info")
dim sql'sql语句
dim taxis'排序的语句
taxis="order by id asc"
dim i'用于循环的整数
start=timer()
datapath="db.mdb"
pages=30

'连接打开数据库

Dim DB    '定义变量
DB="db.mdb"     '定义数据库路径及名称
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(DB)
If Err.Number <> 0 Then
    Response.Write "数据库链接出错!"
    Response.End()
End If

'获取记录总数
sql="select count(id) as idcount from ["& datafrom &"]"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,0,1
idcount=rs("idcount")'获取记录总数

if(idcount>0) then'如果记录总数=0,则不处理
    if(idcount mod pages=0)then'如果记录总数除以每页条数有余数,则=记录总数/每页条数+1
        pagec=int(idcount/pages)'获取总页数
    else
        pagec=int(idcount/pages)+1'获取总页数
    end if

    '获取本页需要用到的id============================================
    '读取所有记录的id数值,因为只有id所以速度很快
    sql="select id from ["& datafrom &"] " & taxis
    set rs=server.createobject("adodb.recordset")
    rs.open sql,conn,1,1

       rs.pagesize = pages '每页显示记录数
       if page < 1 then page = 1
       if page > pagec then page = pagec
       if pagec > 0 then rs.absolutepage = page  

    for i=1 to rs.pagesize
    if rs.eof then exit for  
        if(i=1)then
            sqlid=rs("id")
        else
            sqlid=sqlid &","&rs("id")
        end if
    rs.movenext
    next
    '获取本页需要用到的id结束============================================
end if
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>毛虫的快速分页</title>
<style type="text/css">
<!--
.zw {
    font-size: 12px;
    line-height: 150%;
}
-->
</style>
<script language="javascript">
<!--
function gopage() {
//毛虫的快速分页
//mail:mc@flashado.com
//主页: http://www.flashado.com
//qq:69862476
//本分页供初学者学习,技术上有不当之处,还请各位大侠修正
window.location.href="<%=myself%>?page="+ page.value;
}
//-->
</script>
</head>

<body bgcolor="#f2f2f2" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" height="100%" border="0" cellpadding="20" cellspacing="0">
  <tr>
    <td valign="middle"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
      <tr>
        <td valign="top" bgcolor="#ffffff"><br>          <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="zw">
          <tr>
            <td><strong><font color="#ff6600">毛虫的快速分页</font></strong></td>
          </tr>
        </table>
          <br>
          <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="cccccc" class="zw">
            <tr align="center" bgcolor="#9fcb07">
              <td width="9%"><strong>id</strong></td>
              <td width="37%"><strong>主题</strong></td>
              <td width="33%"><strong>内容(显示前20个字)</strong></td>
              <td width="21%"><strong>时间</strong></td>
            </tr>
<%
if(idcount>0 and sqlid<>"") then'如果记录总数=0,则不处理
    '用in刷选本页所语言的数据,仅读取本页所需的数据,所以速度快
    sql="select [id],[aaaa],[bbbb],[cccc] from ["& datafrom &"] where id in("& sqlid &") "&taxis
    set rs=server.createobject("adodb.recordset")
    rs.open sql,conn,0,1

    while(not rs.eof)'填充数据到表格
    %>
            <tr bgcolor="#ffffff">
              <td align="center"><%=rs(0)%></td>
              <td><%=rs(1)%></td>
              <td><%=left(rs(2),20)%></td>
              <td align="center"><%=rs(3)%></td>
            </tr>
    <%
        rs.movenext
    wend
    %>
          </table>
          <br>
          <table width="90%" border="0" align="center" cellpadding="2" cellspacing="0" class="zw">
            <tr align="center">
              <td align="left">共有<strong><font color="#ff6600"><%=idcount%></font></strong>条记录,<strong><font color="#ff6600"><%=page%></font></strong>/<%=pagec%>,每页<strong><font color="#ff6600"><%=pages%></font></strong>条。</td>
              </tr>
          </table>          
          <table width="90%" border="0" align="center" cellpadding="2" cellspacing="0" class="zw">
            <tr align="center">
              <td align="right">
              <%
    '设置分页页码开始===============================
    pagenmin=page-pagenc'计算页码开始值
    pagenmax=page+pagenc'计算页码结束值
    if(pagenmin<1) then'如果页码开始值小于1则=1
        pagenmin=1
    end if

    if(page>1) then'如果页码大于1则显示(第一页)
        response.write ("<a href='"& myself &"?page=1'><font color='#000000'>第一页</font></a>&nbsp;")    
    end if
    if(pagenmin>1) then'如果页码开始值大于1则显示(更前)
        response.write ("<a href='"& myself &"?page="& page-(pagenc*2+1) &"'><font color='#000000'>更前</font></a>&nbsp;")
    end if

    if(pagenmax>pagec) then'如果页码结束值大于总页数,则=总页数
        pagenmax=pagec
    end if

    for i = pagenmin to pagenmax'循环输出页码
        if(i=page) then
        response.write ("<font color='#ff6600'><strong>"& i &"</strong></font>&nbsp;")
        else
        response.write ("[&nbsp;<a href="& myself &"?page="& i &"><font color='#000000'>"& i &"</font></a>&nbsp;]&nbsp;")
        end if
    next
    if(pagenmax<pagec) then'如果页码结束值小于总页数则显示(更后)
        response.write ("<a href='"& myself &"?page="& page+(pagenc*2+1) &"'><font color='#000000'>更后</font></a>&nbsp;")
    end if
    if(page<pagec) then'如果页码小于总页数则显示(最后页)    
        response.write ("<a href='"& myself &"?page="& pagec &"'><font color='#000000'>最后页</font></a>&nbsp;")
    end if
    '设置分页页码结束===============================
    %>
             转到
            <input name="page" type="text" value="<%=page%>" size="5">页
            <input type="button" name="submit" value="跳转" οnclick="gopage()"></td>
              </tr>
          </table> 
<%
end if
%>
          <br>
          <table width="90%" border="0" align="center" cellpadding="2" cellspacing="0" class="zw">
  <tr>
    <td align="center">
<%
endt=timer()
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
      <%=formatnumber((endt-start)*1000,3)%>毫秒 <br>
      这里可能是0毫秒,但这并不是说这东西真正的0。 </td>
  </tr>
</table>
<br></td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

转载于:https://www.cnblogs.com/xnxqs/archive/2005/06/15/174775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
三大框架分页的实现: DAO里写法: //查出页面要显示的字段 -----分页代码 页面上的查询全部 public List<TblNews> page(Integer pageno) { // TODO Auto-generated method stub log.debug("find TblNewsclass instance"); try { String sql = "select new TblNews(id,title,pubdate,status) from TblNews"; Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql); query.setFirstResult(pageno); query.setMaxResults(3); //设置每页显示的条数 List result = query.list(); log.debug("delete successful"); return result; } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public Integer getRows() {//查询出数据库里的条数 // TODO Auto-generated method stub log.debug("find TblNewsclass instance"); try { String sql = "select count(*) from TblNews"; Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql); List list = query.list(); log.debug("delete successful"); return ((Long)list.get(0)).intValue(); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } Action里的代码实现: private Integer totalElements;//总记录数 private Integer pageNow=1;//当前页 private Integer pageSize=3;//当前页面的记录数 private Integer pageCount;//总页数 private Integer no; 上面就个成员变量再给个set()get()方法; //查询全部 public String findAll()throws Exception{ List<TblNews> list = newsService.page(getRowsno()); //参数是算出的页面的总条数 if(list.size()!=0){ ActionContext.getContext().put("news",list); return "findAllSuccess"; }else{ return ERROR; } } //分页算法 public Integer getRowsno(){ Integer totalElements = newsService.getRows(); pageCount = (totalElements + pageSize - 1) / pageSize;//计算出总页数 if(pageNow <1){ pageNow = 1; } if(pageNow > pageCount){ pageNow = pageCount; } no = (pageNow -1)*pageSize;//实际的记录开始数 return no; } Jsp页面写法: <div id="page"> <s:property value="pageNow"/>/<s:property value="pageCount"/>页 <s:url id="url_pre" value="news_findAll.action"> <s:param name="pageNow" value="pageNow-1"></s:param> </s:url> <s:url id="url_next" value="news_findAll.action"> <s:param name="pageNow" value="pageNow+1"></s:param> </s:url> <s:url id="url_first" value="news_findAll.action"> <s:param name="pageNow" value="1"></s:param> </s:url> <s:url id="url_last" value="news_findAll.action"> <s:param name="pageNow" value="pageCount"></s:param> </s:url> <s:a href="%{url_first}">首页</s:a> <s:if test="pageNow>1"> <s:a href="%{url_pre}">上一页</s:a> </s:if> <s:else> 上一页 </s:else> <s:if test="pageNow!=pageCount"> <s:a href="%{url_next}">下一页</s:a> </s:if> <s:else> 下一页 </s:else> <s:a href="%{url_last}">尾页</s:a> </div> 配置文件里写 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 新闻类型 --> <bean id="newsclassDAO" class="com.cstp.dao.impl.TblNewsclassDAO"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 新闻类型 --> <bean id="newsclassService" class="com.cstp.service.impl.TblNewsclassService"> <property name="classdao"> <ref bean="newsclassDAO"/> </property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="attachDirty*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> //tx标签代表时间传播 <aop:config>//切入 <aop:pointcut id="allServiceMethod" expression="execution(* com.cstp.service.*.*(..))"/> <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice"/> </aop:config> <!-- 新闻类型 --> <bean id="newsclassAction" class="com.cstp.web.NewsclassAction"> <property name="newsclassService"> //这个名字要和action里定义的那个成员变量对应 <ref bean="newsclassService"/> </property> </bean>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值