Link to home
Create AccountLog in
Avatar of DanielAttard
DanielAttardFlag for Canada

asked on

Dynamically generate HTML table with 4, 5 or 6 columns

I need some help wrapping my head around how to structure this particular query.  I am new to HTML coding, but hopefully this can be done without too much difficulty.  

I want to dynamically generate one of three different possible table structures like this:  

If recordset has 1 record,  table needs 4 columns (3 th scope="col", 1 td)
elseIf recordset has 2 records, table needs 5 columns (3 th scope="col", 2 td)
elseIf recordset has 2 records, table needs 6 columns (3 th scope="col", 3 td)

I've shown the three different table layouts that I need below.  

Can someone please suggest an appropriate looping mechanism to achieve this objective?  

<!--Sometimes I need 1 data column:-->
<table class="taxtable">
    <tr>
    	<th scope="col">Header_A</th>
        <th scope="col">Header_B</th>
        <th scope="col">Header_C</th>
        <td>Data_1</td> 
    </tr>
    <tr>
    	<th scope="col">Header_A</th>
        <th scope="col">Header_B</th>
        <th scope="col">Header_C</th>
        <td>Data_1</td> 
    </tr>
    <tr>
    	<th scope="col">Header_A</th>
        <th scope="col">Header_B</th>
        <th scope="col">Header_C</th>
        <td>Data_1</td> 
    </tr>
</table>

<!--Sometimes I need 2 data columns:-->
<table class="taxtable">
    <tr>
    	<th scope="col">Header_A</th>
        <th scope="col">Header_B</th>
        <th scope="col">Header_C</th>
        <td>Data_1</td> 
        <td>Data_2</td> 
    </tr>
    <tr>
    	<th scope="col">Header_A</th>
        <th scope="col">Header_B</th>
        <th scope="col">Header_C</th>
        <td>Data_1</td> 
        <td>Data_2</td> 
    </tr>
    <tr>
    	<th scope="col">Header_A</th>
        <th scope="col">Header_B</th>
        <th scope="col">Header_C</th>
        <td>Data_1</td> 
        <td>Data_2</td> 
    </tr>
</table>

<!--Sometimes I need 3 data columns:-->
<table class="taxtable">
    <tr>
    	<th scope="col">Header_A</th>
        <th scope="col">Header_B</th>
        <th scope="col">Header_C</th>
        <td>Data_1</td> 
        <td>Data_2</td> 
        <td>Data_3</td> 
    </tr>
    <tr>
    	<th scope="col">Header_A</th>
        <th scope="col">Header_B</th>
        <th scope="col">Header_C</th>
        <td>Data_1</td> 
        <td>Data_2</td> 
        <td>Data_3</td> 
    </tr>
    <tr>
    	<th scope="col">Header_A</th>
        <th scope="col">Header_B</th>
        <th scope="col">Header_C</th>
        <td>Data_1</td> 
        <td>Data_2</td> 
        <td>Data_3</td> 
    </tr>
</table>

Open in new window

SOLUTION
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Here is the algorythm
1) Grab the recordset
2) Get the recordset.count (how many records did it return
3) Use a case statement to assign an outer loop iteration for collumns
4) loop the appropriate collumns

It would lok something similar to this in psuedo code:
int intRecordsReturned;
int myCollumns;

intRecordsReturned = getRecordset.count;

Case intRecordsReturned= 1, myCollumns = 1, and print appropriate table header
else Case = 2 and print appropriate table header;

for i=0; i <= myCollumns; i++)
{
   printhtml("<td>" & data variables content blah blah & "</td>");
}

print(<tr>);

Open in new window

of course this would be done in a web scripting lanaguage like jsp, asp.net or CF etc.

I hope this helps
oops, I didn't include the outer tr loop...

I should be something like:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
 int intRecordsReturned;
int myCollumns;

intRecordsReturned = getRecordset.count;

Case intRecordsReturned= 1, myCollumns = 1, and print appropriate table header
else Case = 2 and print appropriate table header;

for j=0; j<=intRecordsReturned; j++)
{
print <TR>
for i=0; i <= myCollumns; i++)
{
   printhtml("<td>" & data variables content blah blah & "</td>");
}
print(</tr>);

}//end outer loop


Also another way to do it is, you can set your application and business rules up in a db table then call the rule, then that would eliminate the need for a case statement, but it would create another call to the db.
 
@DanielAttard: Can you please post the CREATE TABLE statements for the data base tables that are involved here, and give us a "50,000-foot" view of the application?  Tell us what the data is about and how you want to arrange it on the screen, etc.  Then we can provide more concrete help.  Thanks, ~Ray
Avatar of DanielAttard

ASKER

@Ray_Paseur: Here is my overview of what I am trying to accomplish.  I am trying to display some complicated calculations involving about 26 steps from A to Z, but there are three different categories of calculations.  The different categories consist of "Commercial", "Industrial" and "Multi-Residential".  Each row of data will contain three header columns that are constant, plus one or more data columns.  The fourth column will be the first of three possible data columns because sometimes I need to display calculations for one category, sometimes for two, and sometimes for all three categories.

The comments posted by EaswaranP and byten look like something I might be able to work with, because I understand their approach.

Incidentally, I just noticed a type in my original question in my last elseif statement.  The last row is when the recordset has three records and should read like this:

If recordset has 1 record,  table needs 4 columns (3 th scope="col", 1 td)
elseIf recordset has 2 records, table needs 5 columns (3 th scope="col", 2 td)
elseIf recordset has 3 records, table needs 6 columns (3 th scope="col", 3 td)

Just wondering which approach I should jump into and start trying . . .
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Interesting approach.  I think that makes the most sense.  I'll play around with that idea and let you know how I make out.  Thanks for the input.
It should work out fine.  I've done it lots of times.  Please post back if you have any issues with it, or other questions about it.