I've to print a table using TCPDF for creating a PDF using PHP from an array of contents.
In my example I'll use 7 entries, but can be more or less.
$array = array(
0 => "[ ] Option 1",
1 => "[x] Option 2",
2 => "[ ] Option 3",
3 => "[ ] Option 4",
4 => "[x] Option 5",
5 => "[ ] Option 6",
6 => "[ ] Option 7",
...
);
My table layout is dynamic: I can set numbers of columns for display purpose ( $num_cols
can be 1, 2, 3 or 4 columns)
Based on my $num_cols, I've to split my content of the array in each TD cell.
So, if I set $num_cols = 4
, i need to show something like that:
<table>
<tr>
<td>[ ] Option 1</td>
<td>[x] Option 2</td>
<td>[ ] Option 3</td>
<td>[ ] Option 4</td>
</tr>
<tr>
<td>[x] Option 5</td>
<td>[ ] Option 6</td>
<td>[ ] Option 7</td>
<td></td>
</tr>
</table>
I tried to start some ideas but in this example I can get only a row. I don't know how to create the external loop:
echo "<table>";
echo "<tr>";
for ( $i = 0; $i < $num_cols; $i++ ) {
echo "<td>";
if ( isset($array[$i]) {echo $array[$i];} else {}
echo "</td>";
}
echo "</tr>";
echo "</table>";