[Classic ASP]Paged[First Prev 1 2 3 4 5 Next Last]

 [Paged with query]

<%

' set some variables

currentPage = CInt(Request.QueryString("page"))

itemsPerPage = 10


' connect to database and get total items count

Set conn = Server.CreateObject("ADODB.Connection")

conn.Open "your_connection_string"

Set rs = conn.Execute("SELECT COUNT(*) AS TotalItems FROM your_table")

totalItems = rs("TotalItems")

rs.Close


' calculate total pages

totalPages = Ceiling(totalItems / itemsPerPage)


' calculate paging links

firstPage = 1

previousPage = currentPage - 1

nextPage = currentPage + 1

lastPage = totalPages


' make sure previous and next links don't go out of bounds

If previousPage < 1 Then previousPage = 1

If nextPage > totalPages Then nextPage = totalPages


' output paging links

Response.Write("<div>")


' "first" link

If currentPage > 1 Then

  Response.Write("<a href=""?page=" & firstPage & """>first</a> ")

End If


' "prev" link

If currentPage > 1 Then

  Response.Write("<a href=""?page=" & previousPage & """>prev</a> ")

End If


' numeric links

For i = 1 To totalPages

  If i = currentPage Then

    Response.Write("<b>" & i & "</b> ")

  Else

    Response.Write("<a href=""?page=" & i & """>" & i & "</a> ")

  End If

Next


' "next" link

If currentPage < totalPages Then

  Response.Write("<a href=""?page=" & nextPage & """>next</a> ")

End If


' "last" link

If currentPage < totalPages Then

  Response.Write("<a href=""?page=" & lastPage & """>last</a> ")

End If


Response.Write("</div>")


' get data for current page

startIndex = (currentPage - 1) * itemsPerPage

Set rs = conn.Execute("SELECT * FROM your_table ORDER BY some_column OFFSET " & startIndex & " ROWS FETCH NEXT " & itemsPerPage & " ROWS ONLY")


' output data

Do Until rs.EOF

  ' output row data here...

  rs.MoveNext

Loop


rs.Close

conn.Close

Set rs = Nothing

Set conn = Nothing

%>



댓글

가장 많이 본 글