Back to Top
The nuts and bolts for
Creative web development

Paging and caching large record sets

“How to optimise this process both for the person doing the viewing and for the server processing the page.”

Today we look at viewing large record sets, and how to optimise this process both for the person doing the viewing and for the server processing the page. To handle the former, we'll use a technique known as paging, and for the latter, caching. This caching is not the client side browser caching we've all come to know and hate; this caching is done entirely on the server.

 

The code supplied is half the length of the Dreamweaver-generated code, and is hugely beneficial if you have a site where users look through large lists of data without amending it.

Advertisement DMXzone Paginator ASP

Add the coolest page navigation to your site and make browsing through large lists of items or tables easy and quick. Choose from 22 different styles, used by many sites such as Digg, Yahoo, Flickr and much more, to fit perfectly with your design and display the total number of items.

All paging styles are fully CSS based, so you can always fine tune the colors and borders to your site design.

 

6. Display paging links

This section would only render if paging is true.

    <%
    if ( paging ) {
    %>

If so, we determine if we display the Previous link first. We display it if currentPage is bigger than 0, indicating that we are currently viewing page 2 or higher. We set the Previous link to open the same page with the page querystring variable to equal currentPage minus one.

    <%
     if ( currentPage > 0 ) {
    %>
     <a href="PagingSample.asp?page=<%= currentPage - 1 %>">Previous</a>
    <%
     }

Similarly, we check if we need to display the Next link, by checking whether nextPage is smaller than totalPages. The logic here is, if the next page's index is smaller than the total number of pages, we are not currently on the last page. The reason for this is that our currentPage variable starts counting from 0 based whereas our totalPage starts counting from 1. If this is the case, we render the Next link in the same fashion as the Previous link, using nextPage as our page querystring variable.

    if ( nextPage < totalPages ) {
    %>
     <a href=" PagingSample.asp?page=<%= nextPage %>">Next</a>
    <%
     }
    %>

7. Render the data

Finally! After all that work, we can now display the data. Firstly, we decrement orderIndex by 1. You'll see why in a bit. Next, we start the loop:

    <%
    orderIndex--;
    while ( ++orderIndex < endPoint ) {

As you can see, we increment orderIndex by one and then check to see if it is less than endPoint. The important bit here is that orderIndex increments before the comparison, not after, as with a traditional for loop. We do it this way to reduce the number of calculations in the comparison stage of the loop. Bear with me!

If we increment orderIndex after the comparison, we wouldn't see the last record in the range, because we'd reach our endPoint one loop too quickly. Ok, so we can increment endPoint by one before the loop. But have a look at the next line of code, the one which retrieves the record from our orders array:

     order = orders[ orderIndex ];
    %>

The way we have it now, orderIndex is perfectly positioned to begin with the right record. If any of this doesn't make sense to you, I urge you to play around it and see how these variables interact. Anyhow, this is why we decrement once before we begin the loop.

To continue, as seen above, we create a local (to the loop) reference, order, to the current record, based on orderIndex. We then render out the HTML table row, using those nice easy to read names we assigned each field earlier.

     <tr>
     <td><%= order.OrderDate %></td>
     <td><%= order.Client %></td>
     <td align="right"><%= order.Value %></td>
     <td align="right"><%= order.Products %></td>
     </tr>

We close the loop, and render out the remainder of the HTML for the output. Voila! A cached, paged recordset viewer!

Robert Stuttaford

Robert StuttafordI live in Cape Town, South Africa. I've been pushing 1's and 0's ever since my first computer at the age of 11. Ever since then I've always known I'll be a geek. I now work for Wireframe Studio in sunny Cape Town, and have been for the last 3 years. I'm the database / ASP / dynamic Flash guy here. I'm also one of the XHTML / CSS guys. I have alot of fun in my work because I develop solutions in a variety of ways. I always have something to do, and one project is always different to the next. I actually do have a girlfriend amongst all that!

See All Postings From Robert Stuttaford >>

Share this Article