Monday, 18 April 2011

ASP: resulting recordset in two columns

   


Today I am going to show you a little trick: how to display your recordset data in two columns. And to do so, we will use the MOD remainder function.
Let me make things more clear. We assume we are getting data from a database consisting in a single column recordset. Something like:

Name 1
Name 2
Name 3
Name 4
Name 5

Regardless the number of rows, what we want is to display data the following way:

Name 1Name 2
Name 3Name 4
Name 5

How do we do it? Believe me it is very simple.
Some background
First of all let me tell you that if you follow the examples in this post, you will be able to manipulate data in every way you like. You will be able to create tables with any number of columns (even or odd). It is just a matter of understanding the basics that I will explain here.
In order to achieve our goal, we are going to use the MOD function.

The MOD function
The MOD function provides the remainder value in a division. The remainder is what's left over after you do a division:
10 / 2 = 5, remainder 0
18 / 5 = 3, remainder 3
20 / 3 = 6, remainder 2
In ASP, we use the MOD function as if two values are compared:
remainder_value = val1 MOD val2
What is the remainder_value in the following example?
Dim remainder_value
Dim val1
Dim val2
val1 = 10
val2 = 3
remainder_value = val1 MOD val2
The answer is 1.

The table
Just to begin, we will assume some things. First of all, our recordset is RS (wow that's new, isn't it?). Second: we have a repeat region and we set a variable for the number of rows (numRows) to 1, outside our repeat region.
Let's start building our table:
<table border="0" align="left" cellpadding="0" cellspacing="10" width="100%">
                <tr>
                  <td width="50%"></td>
                  <td width="50%"></td>
                </tr>
The table has width = 100% (the other properties are obviously customizable to your needs). As you can see we have already create a dummy row with two columns. Now the first tricky part:
                <tr>
                  <td><%
While ((numRows <> 0) AND (NOT RS.EOF))
%>
                  <%If numRows MOD 2 <> 0 then%>
                  <%response.write "</td></tr><tr>"%>
                  <%end if%>
Here we start with a new row (<tr><td>) and start the repeat region (While). Then, if the number of rows / 2 has a remainder not equal to zero, we close the row. Following the repeat region:
numRows = 1 / 2, remainder <> 0
numRows = 2 / 2, remainder = 0
numRows = 3 / 2, remainder <> 0
numRows = 4 / 2, remainder = 0
And so on.
Now, we create the main container:
<td align="left" width="50%">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
                      <tr>
                        <td><%=(RS.Fields.Item("name").value)%></td>
                      </tr>
</table>
</td>
And then again, we check the closing tags:
                  <%If numRows MOD 2 = 0 then%>
                  <%response.write "</tr><tr><td>"%>
                  <%end if%>
                  <%
  numRows=numRows+1
  RS.MoveNext()
Wend %>
</table>
Basically, we close the row when the numRows MOD 2 is equal to zero. Outside the repeat region, we close the table tag. And the result is what we were looking for.
As an exercise, think about doing the same for 3 columns table. If you can't find the solution, please ask and I might publish a new post with the solution.

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.