#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Posts
    30
    Rep Power
    13

    dynamic table help


    I am completely new to jsp development. Anyhow, my company has now switched technologies, and I am trying to make the switch. I have been trying to create a dynamic table that is always 2 columns wide and has as many rows as necessary to contain all of the output. So far, I have been able to get the table to print each piece of output in two columns like this (the code that creates this output is posted below):

    category1 category1
    category2 category2
    category3 category3

    Or, if I change the (i)'s in <% out.print(browserCategoryTree.getLevelURL(i)); %> and <% out.print(browserCategoryTree.getLevelAttributeString(i,"categoryName") %> to (j)'s I get the following:

    category1 category2
    category1 category2
    category1 category2

    What I would like to have happen is the to have a table display the following in two rows and two columns:

    category1 category2
    category3


    Here is the code:

    <% // This is the BEGINNING of the table loop for the sub-categories. %>

    <% if( browserCategoryTree.numberOfCategories() > 0 ) { %>
    <table width="480" border="1" cellspacing="0" cellpadding="0" align="center">
    <%
    int columns = 2;
    for( int i=0; i < browserCategoryTree.numberOfCategories(); i++ ) {
    %>
    <tr>
    <% for( int j=0; j < columns; j++ ) { %>
    <td class="whiteSmallFont" width="240">
    &nbsp;&nbsp;&nbsp;<a href="<% out.print(browserCategoryTree.getLevelURL(i)); %>"><% out.print(browserCategoryTree.getLevelAttributeString(i,"categoryName")); %></a>
    </td>
    <% } %>
    </tr>
    <% } // END for loop %>

    </table>

    <% // This is the END of the table loop for the sub-categories. %>


    Does anyone have any suggestions (sample code, websites, articles, career switch ), I am really struggling with this.

    Thanks,
    designdawg
    designdawg
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    For what you want to do you do not need a nested for loop, one will suffice. Something like this maybe (note: this code is not tested):
    Code:
    <% 
    int numCats = browserCategoryTree.numberOfCategories();
    if( numCats > 0 ) { 
    %>
    <table width="480" border="1" cellspacing="0" cellpadding="0" align="center">
    <% for( int i=0; i < numCats; i++ ) { %> 
    <tr>
    <td class="whiteSmallFont" width="240">
       <a href="<% out.print(browserCategoryTree.getLevelURL(i)); %>">
    	<% out.print(browserCategoryTree.getLevelAttributeString(i,"categoryName")); %></a>
    </td>
    	<% i++;
    	      if(i < numCats) { %>
    <td class="whiteSmallFont" width="240">
       <a href="<% out.print(browserCategoryTree.getLevelURL(i)); %>">
    	<% out.print(browserCategoryTree.getLevelAttributeString(i,"categoryName")); %></a>
    </td>
    	<% } %>
    </tr>
    <% } // END for loop 
    } //end if %>
    </table>

IMN logo majestic logo threadwatch logo seochat tools logo