dimanche 17 avril 2011
ASP: resulting recordset in two columns
Do you like this story?
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 1 | Name 2 | 
| Name 3 | Name 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 val2Dim remainder_value
Dim val1
Dim val2
val1 = 10
val2 = 3
remainder_value = val1 MOD val2The 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>                <tr> 
                  <td><% 
While ((numRows <> 0) AND (NOT RS.EOF)) 
%> 
                  <%If numRows MOD 2 <> 0 then%> 
                  <%response.write "</td></tr><tr>"%> 
                  <%end if%>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>                  <%If numRows MOD 2 = 0 then%> 
                  <%response.write "</tr><tr><td>"%> 
                  <%end if%> 
                  <% 
  numRows=numRows+1
  RS.MoveNext()
Wend %>
</table>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.

This post was written by: Franklin Manuel
Franklin Manuel is a professional blogger, web designer and front end web developer. Follow him on Twitter
Inscription à :
Publier les commentaires (Atom)


0 Responses to “ASP: resulting recordset in two columns”
Enregistrer un commentaire