0

I want to create a table with html tags as strings, passing in the source code values from a 2d array. I create a string and inside it I pass my variables The desired format of the table data part is the following

<tr><td><td>CONTACT1&nbsp;</td><td>125&nbsp;</td><td>1&nbsp;</td></td></tr>
<tr><td><td>CONTACT2&nbsp;</td><td>126&nbsp;</td><td>2&nbsp;</td></td></tr>
<tr><td><td>CONTACT3&nbsp;</td><td>127&nbsp;</td><td>3&nbsp;</td></td></tr>
<tr><td><td>CONTACT4&nbsp;</td><td>128&nbsp;</td><td>4&nbsp;</td></td></tr>

But instead of this I get the following

<tr><td><td>CONTACT1&nbsp;</td><td>125&nbsp;</td><td>1&nbsp;</td>
<td><td>CONTACT2&nbsp;</td><td>126&nbsp;</td><td>2&nbsp;</td>
<td><td>CONTACT3&nbsp;</td><td>127&nbsp;</td><td>3&nbsp;</td>
<td><td>CONTACT4&nbsp;</td><td>128&nbsp;</td><td>4&nbsp;</td></td></tr>

I am trying with the following for loop

for (int i = 0; i < PlayerCount; i++)
{

for (int j = 0; j < 12; j++)
    {
      col = "<td>" +
            contactsarray[i][j] +
            "&nbsp;" +
            "</td>";

            mainsource = mainsource + col;

    }
    row="<tr><td>"+mainsource+"</td></tr>"+row;

}

How I am going to change table row and achieve the desired result by putting extra and for each contact?

2
  • Is the result you show from rendered html? I think the problem is you are writing invalid html, because the for loop looks like it would produce the expected result. You can not nest columns inside a column. Is there a reason for wrapping the three columns in each row with a column? Commented Apr 23, 2016 at 14:56
  • The result shown is from my html source code written in notepad Commented Apr 23, 2016 at 14:58

2 Answers 2

1

No its not that, I need to reset the mainsource variable to mainsource=""

for (int i = 0; i < contactsarray.length; i++) //fixed for condition
{
for (int j = 0; j < contactsarray[i].length; j++) //fixed for condition
{
  col = "<td>" +
        contactsarray[i][j] +
        "&nbsp;" +
        "</td>";

        mainsource = mainsource + col;

}
row="<tr>"+mainsource+"</tr>"+row; 

mainsource="";

}
Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem is you are producing invalid html:

<tr><td><td>CONTACT1&nbsp;</td><td>125&nbsp;</td><td>1&nbsp;</td></td></tr>
<tr><td><td>CONTACT2&nbsp;</td><td>126&nbsp;</td><td>2&nbsp;</td></td></tr>
<tr><td><td>CONTACT3&nbsp;</td><td>127&nbsp;</td><td>3&nbsp;</td></td></tr>
<tr><td><td>CONTACT4&nbsp;</td><td>128&nbsp;</td><td>4&nbsp;</td></td></tr>

Columns (td) can not be wrapped with another column. Your for loop conditions could be improved as well to loop through the 2d array properly.

for (int i = 0; i < contactsarray.length; i++) //fixed for condition
{
    var mainsource = ""; //fixed variable declaration

    for (int j = 0; j < contactsarray[i].length; j++) //fixed for condition
    {
      var col = "<td>" + //fixed variable declaration
            contactsarray[i][j] +
            "&nbsp;" +
            "</td>";

            mainsource = mainsource + col;

    }
    row="<tr>"+mainsource+"</tr>"+row; //fixed so you are not wrapping columns with another column

}

Here is some good information on html tables and looping through a 2d array to help you understand:

http://www.w3schools.com/html/html_tables.asp

http://www.plus2net.com/javascript_tutorial/array-two-dimension.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.