I got an html table, and I use some loop to get some data, this data is displaying this way:
<tr><td>Data</td></tr>
... next loop
But I wan't it to close table row (tr) every 2 or even 3 loops. So the data may look like this:
<tr>
<td>Data</td>
<td>Data1</td>
<td>Data2</td>
</tr>
...next loop...
Will you help me with this?
Using the modulo (%) operator is always a great solution for the problem you have above. Since you didn't provide details about the implementation language, I've taken the liberty to provide you with a php example of how it's done.
If you have some counter in your loop you can use Modulus for this.
It's basically what's left of a number if you divide it.
Example:
If you use a
foreach()
in stead you should just make a counter yourself (as Link stated you could also use thekey
of an array if it contains nice incremental keys):Or in your specific case it would look something like:
The above is just a basic example.
You should also check whether the number of the columns is balanced and if not either add a colspan or an empty columns to balance it.