Search Tools Links Login

Paging recordsets from a database (acess) like Google...


This code pages a database recordsets in a very cool way, like the good seaches on the web. The ideia I designed was to show all pages, but not all of them (imagine in a case of 1000 pages). So, it shows only ten pages placed in the point you are only, and a link for the last and first ones... It helps who does not have the patience to see all pages, and also for the ones who does.

Original Author: Bruno Martins Braga

Assumptions

The most interesting of this code is that if you are, for example, in a database with 1000 pages, and you are in the middle of it, it will link you the first page "1", and also link the last "1000"... Also, will show your current location with 5 pages back and 5 pages foward... Isn`t great?

Side Affects

Be carefull to use the Database correctly... and I suppose that at this far you already know how to display your database recordsets... If not, look for examples in this web site.
You also can see this code in action in my personal page : http://www.bmbks.org/personal/index.asp?Idioma=1 (portuguese) - But there is an option to english and japanese (but the huge database is in portugues, sorry. The other languages contain the same idea but the database is still small)

API Declarations

The original code was not mine, but I change it completely from the inicial idea.

Code

<%
'Remember to set correctly your database location, and the contents as well. There are other
'codes in this site to explain how to show a recordset. I supposed this step we can foward...
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "DBQ=" & Server.MapPath("database.mdb") & ";Driver={Microsoft Access Driver (*.mdb)}","username","password"
Dim TotalPages, NumPerPage, CurrentPage, Number
Dim strQuery, ObjRs, Count
'Defines the inicial Value as "5"
If request("NumPerPage") <> 0 then NumPerPage = Int(request("NumPerPage")) else NumPerPage = 5
strQuery = "SELECT * FROM TableName order by data desc"
Set ObjRs = Server.CreateObject("ADODB.Recordset")

If Request.QueryString("page") = "" then
CurrentPage = 1 'We're on the first page
Else
CurrentPage = CInt(Request.QueryString("page"))
End If
objRS.Open strQuery, objConn, 1, 1 'Opened as Read-Only
Number = objRS.RecordCount
If Not objRS.EOF Then
objRS.MoveFirst
objRS.PageSize = NumPerPage
TotalPages = objRS.PageCount
objRS.AbsolutePage = CurrentPage
End If
Dim ScriptName
ScriptName = request.servervariables("ScriptName")
%>


<%
While Not objRS.EOF and Count < objRS.PageSize
count = count + 1
%>
Put here your RecordSet Display (usually a table with the ASP code together)
<%
objRS.MoveNext
Wend
objRs.close
Set objRs = Nothing
%>
<%
'Print the recent Data
response.write "Showing page " & CurrentPage & " of " & TotalPages & ": Total of " & Number & " written posts..."
%>
<%
'Creating the paging numbers
Dim ini, fim, a
'Display PREV page link, if appropriate
If Not CurrentPage = 1 Then
Response.Write "<a href='" & ScriptName & "?NumPerPage=" & NumPerPage & "&page=" & CurrentPage - 1 & "'>..</a>  "
if CurrentPage > 5 and TotalPages > 10 then
Response.write("<a href=" & ScriptName & "?NumPerPage=" & NumPerPage & "&page=1>1</a>" & " ...  " )
end if
if TotalPages > 10 then

if CurrentPage > 5 then
if TotalPages > (CurrentPage + 5) then
ini = (CurrentPage - 4)
fim = (CurrentPage + 5)
else
ini = (TotalPages - 9)
fim = TotalPages
end if
else
ini = 1
fim = 10
end if

else
ini=1
fim = TotalPages
end if
For a = ini to fim
If a = Cint(request("page")) then
Response.write( "" & a & "  ")
Else
Response.write("<a href=" & ScriptName & "?NumPerPage=" & NumPerPage & "&page=" & a &">" & a & "</a>" & "  " )
End if
Next
Else
if TotalPages = 1 then
Response.write ""
Else
Response.Write "1  "
End if
if TotalPages > 10 then
fim = 10
else
fim = TotalPages
end if
For a = 2 to fim
If a = Cint(request("page")) then
Response.write( "" & a & "  ")
Else
Response.write("<a href=" & ScriptName & "?NumPerPage=" & NumPerPage & "&page=" & a &">" & a & "</a>" & "  " )
End if
Next
End If
if CurrentPage < TotalPages - 5 and TotalPages > 10 then
Response.write("... <a href=" & ScriptName & "?NumPerPage=" & NumPerPage & "&page=" & TotalPages &">" & TotalPages & "</a>" & "  " )
end if
'Display NEXT page link, if appropriate
If Not CurrentPage = TotalPages Then
Response.Write "<a href='" & ScriptName & "?NumPerPage=" & NumPerPage & "&page=" & CurrentPage + 1 & "'>..</a>"
Else
Response.Write ""
End If
%>

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 109 times

Categories

ASP/ HTML

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.