You are here: Home > Articles > Article Display

GetRows VBScript Class - Part III: Paging the results

In Part I of this series, we saw how to create a VBScript class to query our database using the very fast GetRows() method, and return a recordset as a local array. In Part II, we extended the class to allow ADDing and UPDATEing a row in the database. In this Part III, we will expand the class further to allow pagination of the returned recordset.

Published: Jan 16, 2003
Tested with: ASP 3.0, VBScript 5.5
Category: ASP
28,489 views

This article is Part 3 of 3 total articles in the series. The rest are:

Introduction

In Part I of this series, we saw how to create a VBScript class to query our database using the very fast GetRows() method, and return a recordset as a local array. In Part II, we extended the class to allow ADDing and UPDATEing a row in the database. In this Part III, we will expand the class further to allow pagination of the returned recordset.

Why GetRows()?

This is an obvious question to ask, especially as we move into the third installment of the series. Why persist on using this method with ASP 3.0, when there are many ways to create a recordset and many more to paginate through it? The reason to use this method for the entire class, is that it is the fastest way to get a recordset out. We can call this method, asking it to return only how many records we want, and it will only fetch that many records.

Class Database

We will add numerous new methods and properties to our class to achieve this. Let's first take a look at the complete structure of the class.

Properties
Visibility Type Name Accesibility
public String AlternateRowColor Set
public Integer CellPadding Set
public Integer CellSpacing Set
public Integer CurrentPage Get
public Integer EndingRecord Get
public String Output Get
public String Query Set
public Integer RecordsPerPage Get, Set
public String RowColor Set
public Integer StartingRecord Get
public Integer TotalPages Get
public Integer TotalRecords Get
Methods
Visibility Return Name Parameters
public Boolean AddNew (String table, Array fields, Array values)
private --- Class_Initialize ()
private --- Class_Terminate ()
public String DisplayResults ()
public String GetPagingHTML ()
public Array GetRecordArray (Integer intRecordsToShow)
public String GetRecordPositionHTML ()
public Boolean Update (String table, Double id, Array fields, Array values)

Automatic paging display

Without going through the entire code, let's see the simplest way to query our database and display paginated results using this class. I will be using a simple MS Access database file. All the samples displayed here are available in the download zip file.

1 <html>
2 <head>
3     <title>Paginated Results</title>
4 </head>
5 <body>
6 <!--#Include file="clsDatabase.asp"-->
7 <%
8 Dim clsDatabase
9 Set clsDatabase = New Database
10     clsDatabase.Query = "SELECT title AS [Title], description AS [Description], posted AS [Posted] FROM article ORDER BY posted DESC"
11     clsDatabase.DisplayResults()
12 Set clsDatabase = Nothing
13 %>
14 </body>
15 </html>

First we include the file which has the class code, clsDatabase.asp. Then we create a new instance of the class, and set the Query property of the class to a SQL query. The DisplayResults() method puts everything in a pre-formatted table and returns the full HTML code back to the browser. Our page will now look like this:

Default pagination results

By default, the class shows 5 records per page. The first row of the table displays the current resultset location. Then follow the column titles. The rows with the results alternate in colors to improve visibility. The last row of the table shows what page we are on and allows us to move to another page.

Changing the look of the default table

The class allows us to slightly alter the look of the default table. We can change the cellpadding, cellspacing, the row color, and the alternating row color of the results. Let's see the code to do this:

1 <html>
2 <head>
3     <title>Paginated Results</title>
4 </head>
5 <body>
6 <!--#Include file="clsDatabase.asp"-->
7 <%
8 Dim clsDatabase
9 Set clsDatabase = New Database
10     clsDatabase.Query = "SELECT title AS [Title], description AS [Description], posted AS [Posted] FROM article ORDER BY posted DESC"
11     clsDatabase.RecordsPerPage = 4
12     clsDatabase.RowColor = "gold"
13     clsDatabase.AlternateRowColor = "#FFFFDC"
14     clsDatabase.CellPadding = 2
15     clsDatabase.CellSpacing = 1
16     clsDatabase.DisplayResults()
17 Set clsDatabase = Nothing
18 %>
19 </body>
20 </html>

The code above will produce the following display:

Edited paginated display

Complete customization of the display

Although the DisplayResults() method is simple to use, there might be cases where we need to customize the look of the table even more. For example, we might want to set the width of a particular column. One way to do this is to edit the class code and maybe add more properties. Another way is to move the customization to the presentation layer of ASP. Let's see how we can use the rest of the properties of the class to create a very flexible paging view of our recordset.

1 <html>
2 <head>
3     <title>Paginated Results</title>
4 </head>
5 <body>
6 <!--#Include file="clsDatabase.asp"-->
7 <table border="1" cellpadding="3" cellspacing="1" borderColorLight="#666666" borderColorDark="#ffffff">
8 <%
9 Dim clsDatabase
10 Dim lclArray
11 Set clsDatabase = New Database
12     clsDatabase.Query = "SELECT title, description, posted FROM article ORDER BY posted DESC"
13     lclArray = clsDatabase.GetRecordArray(4)
14 %>
15     <tr>
16         <td align="center" bgcolor="#CCCCCC" colspan="3">
17             <%=clsDatabase.GetRecordPositionHTML()%>
18         </td>
19     </tr>
20     <tr>
21         <td align="center" bgcolor="#ffffcc" colspan="3">
22             Results <%=clsDatabase.StartingRecord%> - <%=clsDatabase.EndingRecord%> of <%=clsDatabase.TotalRecords%>
23         </td>
24     </tr>
25     <tr bgcolor=#cc9933>
26         <td>Title</td>
27         <td>Article Description</td>
28         <td>Posted</td>
29     </tr>
30 <%
31 Dim rowCounter
32 Dim strBgColor
33 For rowCounter = 0 To UBound(lclArray,2)
34     If rowCounter Mod 2 Then
35         strBgColor = "#CCCC99"
36     Else
37         strBgColor = "#FFFFFF"
38     End If
39     Response.Write("<tr bgcolor=""" & strBgColor & """><td width=""150"">" _
40         & lclArray(0,rowCounter) & "</td>" _
41         & "<td>" & lclArray(1,rowCounter) & "</td>" _
42         & "<td nowrap>" & lclArray(2,rowCounter) & "</td></tr>")
43 Next
44 %>
45     <tr>
46         <td bgcolor="#CCCCCC" colspan="3">
47             <%=clsDatabase.GetPagingHTML()%>
48         </td>
49     </tr>
50     <tr>
51         <td bgcolor="#ffffcc" colspan="3">
52 <%
53 Dim strOutput
54 Dim strThisPage
55 Dim i
56 strThisPage = Request.ServerVariables("SCRIPT_NAME")
57 strOutput = "Result Page:"
58 If clsDatabase.CurrentPage <> 1 Then
59     strOutput = strOutput & "&nbsp;&nbsp;<a href=""" & strThisPage & "?StartingRecord=" & ((clsDatabase.CurrentPage - 1) * clsDatabase.RecordsPerPage) - clsDatabase.RecordsPerPage + 1 & """>Previous</a>&nbsp;&nbsp;"
60 End If
61 For i = 1 To clsDatabase.TotalPages
62     If i <> clsDatabase.CurrentPage Then
63         strOutput = strOutput & "&nbsp;&nbsp;<a href=""" & strThisPage & "?StartingRecord=" & (i * clsDatabase.RecordsPerPage) - clsDatabase.RecordsPerPage + 1 & """>" & i & "</a>&nbsp;&nbsp;"
64     Else
65         strOutput = strOutput & "&nbsp;&nbsp;<b>" & i & "</b>&nbsp;&nbsp;"
66     End If
67 Next
68 If clsDatabase.CurrentPage <> clsDatabase.TotalPages Then
69     strOutput = strOutput & "&nbsp;&nbsp;<a href=""" & strThisPage & "?StartingRecord=" & (clsDatabase.CurrentPage * clsDatabase.RecordsPerPage) + 1 & """>Next</a>&nbsp;&nbsp;"
70 End If
71 Response.Write(strOutput)
72 %>
73         </td>
74     </tr>
75     <tr>
76         <td bgcolor="#ffffcc" colspan="3">
77             Page <%=clsDatabase.CurrentPage%> of <%=clsDatabase.TotalPages%>
78         </td>
79     </tr>
80 </table>
81 <%
82 Set clsDatabase = Nothing
83 %>
84 </body>
85 </html>

So, first we create a local array variable, lclArray, and we populate it using the method GetRecordArray(4). The 4 means that we want to show 4 records per page. The rest of the logic is pretty much the same as the one existing inside the class - we just move it outside for more flexibility. We get the following display from the code above:

Custom pagination

Returning all the records

In the previous series, the way to return all the records of the query was by a custom method which we called GetRows(strQuery). This method, as you may have noticed, no longer exists. It has been replaced with GetRecordArray(intRecordsToShow). This was necessary to accomodate the paging mechanism. So, how do we achieve the same effect now if we want to simply return all the records into a local array?

1 <html>
2 <body>
3 <!--#Include file="clsDatabase.asp"-->
4 <table border="1" cellpadding="3" cellspacing="0">
5 <%
6 Dim clsDatabase
7 Dim lclArray
8 Dim rowCounter
9 Set clsDatabase = New Database
10     clsDatabase.Query = "SELECT title, posted FROM article ORDER BY posted DESC"
11     lclArray = clsDatabase.GetRecordArray(NULL)
12 Set clsDatabase = Nothing
13 For rowCounter = 0 To UBound(lclArray,2)
14 %>
15     <tr>
16         <td><%=lclArray(0, rowCounter)%></td>
17         <td><%=lclArray(1, rowCounter)%></td>
18     </tr>
19 <%
20 Next
21 %>
22 </table>
23 </body>
24 </html>

The code above is using a little trick to pass conditional parameters to the GetRecordArray() method. Since it is not possible with VBScript to pass conditional parameters, we simple pass a NULL value if we want to leave a certain parameter empty. Inside the GetRecordArray() method we have some checking if something is passed or not and if NULL is passed, then we set the parameter to some default value. Feel free to explore this method inside the class to see how it is done. In our class, the normal behavior is to pass the number of records we want to see on each page to this method. Passing a value of NULL, simply makes that number something big, like 3,000 records.

As a result, we get all the records in our database returned and displayed:

Returning all records example

If we wanted to show only the first 3 of the records, we would use the same code as above except for one line:

1 ...
2     lclArray = clsDatabase.GetRecordArray(3)
3 ...

The 3 as a parameter tells the method to retrieve only the first 3 records.

Conclusion

Until now, we could use this class to add a new row to our database, edit a row, and return a recordset in the form of a local array. Now, we can also paginate the returned recordset easily and fast. We can further expand on this class by adding more properties if we like. For example, we saw that moving some of the code outside of the class, we can gain more flexibility in the display of the contents. Instead of doing that, we can leave the code inside the class, and add some more properties, which will allow us to customize the display even more. Examples that could be implemented right away are whether to show the results position, the pagination links, or the column headers. Maybe even be able to add custom titles to the columns, define a width, nowrap, or be able to sort them by ascending or descending order.

 



Other articles in this category
  1. Exporting Word files to HTML
    March 5, 2003
    In this article we will first discuss the case for and against using Word as your HTML editor. Then we will see how to properly save a Word file to smaller, more compact HTML files. Third and last, we will see how to do this through code, and possibly create a batch process for converting numerous Word files to HTML at once.
  2. Dynamic Tree Menu of your site
    May 31, 2002
    We'll see how to create a menu system that is cross-browser and includes all your site's folders/files. It uses ASP, XML and DHTML and by simply copying it to your site you have an instant Windows Explorer-like navigation of the contents.
  3. Generating an XML file of your website's folders/files
    May 24, 2002
    Using the File System Object (FSO) we can traverse through our website's contents and write them out in a nicely nested form in an XML file. We can then use that file for example, in a content management system or a TreeView control.
  4. Downloading any file using ASP, FSO and the ADODB Stream object
    May 8, 2002
    In this article, we will see how to allow a user to download any file from our web server. They will see a prompt, giving them the option of opening or saving it, rather than simply opening it which is the default. We can achieve this using the FSO and ADODB objects.
  5. Calling MS Access Parameterized Queries from ASP
    April 30, 2002
    Instead of passing a SQL query through your ASP code against Microsoft Access as you would normally do, you can use the Queries design interface to create them in Access and then call them from your ASP code. It makes things easier to edit and maintain, and the results are returned faster.