簡單高效asp分頁程式

來源:互聯網
上載者:User

<%@ language = "vbscript" codepage = 936%>
<%
'==========================================================================
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>
<link rel="stylesheet" href="page.css" type="text/css">
<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><%=rs(2)%></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="跳轉" onClick="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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.