现在的位置: 首页 > 综合 > 正文

一个ASP通用分页函数

2013年04月26日 ⁄ 综合 ⁄ 共 2649字 ⁄ 字号 评论关闭
'ASP通用翻页函数 作者:天地小子 2005.7.7 twt326@163.com
'输入查询语句,数据库名,数据库连接,当前页号,链接字符串,列表条数,查询条件(默认为空)
'显示上下页导航链接,有下拉框显示页数
'这个函数是根据天地PHP通用翻页函数1直接转变的,其中使用了CSDN BBS上一个大虾的SQL高效分页方案技术
':)由于这里没有找到那篇帖子,所以老兄请勿见怪了。配合下面的函数使用起来更方便
sub getnav(dbname,conn,tnowpage,link,pagelistnum,wherewords)
  dim sql,trst,sumrows,pagesum,tmphead,showdown,selectcode,i
  sql="select count(*) as recnum from "&dbname
  if wherewords<>"" then sql=sql & " where " & wherewords
  set trst=Server.CreateObject("adodb.recordset")
  trst.open sql,conn,1,1
  sumrows=trst("recnum")
  pagesum=sumrows/pagelistnum+1
  if sumrows mod pagelistnum=0 then pagesum=sumrows/pagelistnum
  tmphead="<a href="&link&"&page="
  if cint(tnowpage)<2 then
    showdown="<table><form name='selform' method='post' action=''><tr><td>首页&nbsp;&nbsp;上页&nbsp;&nbsp;"
  else
    showdown="<table><form name='selform' method='post' action=''><tr><td>"&tmphead&"1>首页</a>&nbsp;&nbsp;"&tmphead&(tnowpage-1)&">上页</a>&nbsp;&nbsp;"
  end if
  if cint(tnowpage)<cint(pagesum) then
    showdown=showdown&tmphead&(tnowpage+1)&">下页</a>&nbsp;&nbsp;"&tmphead&pagesum&">末页</a>&nbsp;&nbsp;"
  else
    showdown=showdown&"下页&nbsp;&nbsp;末页&nbsp;&nbsp;"
  end if
  '获取下拉框转向代码
  selectcode="<script language='javascript'>function gopagenav(page){  location='"&link&"&page='+page;  }</script><select name='selpage' id='selpage' onChange='javascript:gopagenav(this.value);'>"
  for i=1 to pagesum
    selectcode=selectcode&"<option value='"&i&"'"
    if cInt(i)=cint(tnowpage) then selectcode=selectcode&" selected"
    selectcode=selectcode&">= "&i&" =</option>"
  next
  selectcode=selectcode&"</select>"
  showdown=showdown&"第</td><td>"&selectcode&"</td><td>页,共"&pagesum&"页&nbsp;&nbsp;总记录数:"&sumrows&"</td></tr></form></table>"
  response.write showdown
  trst.close
  set trst=nothing
end sub
'根据参数生成查询语句,查询某页数据
'参数:当前页,每页显示条数,数据表名(多个用逗号隔开),需要选择的字段名(用逗号隔开),查询条件(无条件时为空字串),ID主键,排序方式(ASC正序或DESC倒序)
'使用示例
'  sql=getfysql(page,pagelistnum,"tbl_message,tbl_login","tbl_message.*,tbl_login.l_user","tbl_message.m_lid=tbl_login.l_id","m_id","DESC")
'  getnav "tbl_message",conn,page,"adminmessage.asp?act=",pagelistnum,addnavwords
function getfysql(tnowpage,tpagelistnum,tblname,selfields,wherewords,keyfield,sorttype)
  dim fh,hs
  if wherewords="" then wherewords="1=1"
  fh=">":hs="max"
  if sorttype="DESC" then
    fh="<"
hs="min"
  end if
  if (tnowpage-1)*tpagelistnum=0 then
    sql="select top "&tpagelistnum&" "&selfields&" from "&tblname&" where "&wherewords&" order by "&keyfield&" "&sorttype
  else
    sql="select top "&tpagelistnum&" "&selfields&" from "&tblname&" where "&wherewords& " and " &keyfield&fh&"(select "&hs&"("&keyfield&") from (select top "&((tnowpage-1)*tpagelistnum)&" "&keyfield&" from "&tblname&" where "&wherewords& " order by "&keyfield&" "&sorttype&") as T) order by "&keyfield&" "&sorttype
  end if
  getfysql=sql
end function

抱歉!评论已关闭.