Wednesday, October 11, 2006

Using the Mod function in ASP to Create Dynamically Sized Tables

There's a relatively obscure function in ASP called Mod, which is short for "modular division" or "modulo" in Latin. The "modulus", or remainder, is what's left over when one number is divided by a number.

If you not as old as I am (or if you have kids right now) you might remember doing division with a remainder. So 13/6 = 2 R1, where R is the remainder (what's left over after you divide 12 by 6). Well that's what Mod is.

Syntax of the Mod function:
Number 1 mod Number 2
Here's a little loop to show you how the Mod function works to return the remainder:
For i = 1 to 10
Response.write(i & " mod 5 = " & i mod 5 & "<br>")
The results of this will look like:
1 mod 5 = 1
2 mod 5 = 2
3 mod 5 = 3
4 mod 5 = 4
5 mod 5 = 0
6 mod 5 = 1
7 mod 5 = 2
8 mod 5 = 3
9 mod 5 = 4
10 mod 5 = 0
Let's break down some of them:
1 mod 5 = 1 / 5 = 0 with a remainder of 1
2 mod 5 = 1 / 5 = 0 with a remainder of 2
6 mod 5 = 1 / 5 = 1 with a remainder of 1

In the commercial world, many advanced encryption techniques, including the world-standard RSA Encryption Algorithm, use the mod function as part of their internal functioning.

But what use is Mod to you?

The RetroWebDev post <a href=””>Alternating Row Colors</a> is a solution that makes use of Mod.

With a more advanced script, you can use Mod to create dynamically sized tables.

Say you want a table with 3 cells in each row, and there are 9 items to be displayed. Three rows of three and you're done. But suppose there are 10 items. To keep the table well-formed, you need to create three rows of three, then an additional row with one cell containing the 10th item, then two empty cells and a close row tag. Now what if you want to give the user the option of showing 3, 4, or 5 items per row? Mod to the rescue.

Here's a function that will produce a well-formed table based on your input of intCells and intItems. Simply pass in the values you want for number of cells per row and the total number of items:
Function MakeTable(intCells, intItems)

strHTML = "<table cellpadding=2 cellspacing=0>"

'Loop through the items
For i=1 to intItems
' start of new row?
If i mod intCells=1 Then strHTML = strHTML & "<tr>"

' Add a cell
strHTML = strHTML & "<td>" & i & "</td>"

' end of row?
If i mod intCells=0 Then strHTML = strHTML & "</tr>"

'if end of row, fill remainder with empty cells
If intItems mod intCells > 0 Then
' loop to complete table
For j=1 to intCells-(intItems mod intCells)
' add empty cell
strHTML = strHTML & "<td> </td>"

' close row if last cell
If j=intCells-(intItems mod intCells) Then strHTML = strHTML & "</tr>"
End if

' close table
strHTML = strHTML & "</table>"

' assign strHTML to the function
MakeTable = strHTML
End Function
Calling the function:
Call MakeTable(3,9)
creates a table with 3 cells per row and 9 total items.
Call MakeTable(5,21)
creates a table with 5 cells per row and 21 total items (4 rows of 5 and 1 row of 1 with 4 empties).

While you might not be in the cryptography business, if you create shopping carts, online stores, photo albums, calendars, etc., anything that might need the flexibility of tables with variable numbers of items and/or items per row, Mod is a great tool to be familiar with.

No comments: