TCPDF - loop data in two columns
Im using TCPDF, at the moment im listing data in two columns using array_chunk
which works fine. But I need the data to be displayed in the first columns and then in the second, see below:
Currently:
1 2
3 4
5 6
7 8
9 10
Should be:
1 6
2 7
3 8
4 9
5 10
This is the code:
<?php $array = range(1, 50);?>
<table nobr="true" cellpadding="2">
<?php foreach (array_chunk($array, 2) as $a) { ?>
<tr>
<?php foreach ($a as $array_chunk) { ?>
<td><?php echo $array_chunk; ?></td>
<?php
} ?>
</tr>
<?php } ?>
</table>
my second query (complex), if there are more than 30 lines, I should be able to use $ pdf-> AddPage (); and continues on the next page.
source to share
TCPDF - multicolumn support, this is the wat I used to solve my problem:
$pdf->AddPage();
$pdf->resetColumns();
$pdf->setEqualColumns(2, 84); // KEY PART - number of cols and width
$pdf->selectColumn();
$content =' loop content here';
$pdf->writeHTML($content, true, false, true, false);
$pdf->resetColumns()
the code will add an automatic page break and continue to the next page.
source to share
I haven't used PHP in a while, so I'll let you write some code, but hopefully this helps you solve the problem.
I think the one second problem is the simplest one: you can only have 30 lines per page. Since you have 2 elements per line, this means 60 elements per page. So just split the array into arrays of 60 elements per array, something like this, in pseudocode:
items = [1, 2, 3, ...] // an array of items pages = [] i = 0 while 60 * i < items.length pages[i] = items.slice(i * 60, (i + 1) * 60) i = i + 1
The second problem is this: you want to create an output column for each column, but HTML requires you to output line by line. So, before we can print a line, we need to know how many lines we want to print in total:
items = [1, 2, 3, ...] // an array of items
rows = items.length / 2 // The number of rows, make sure you round this right in PHP
n = 0
while n < rows
// The n:th item of the first column
print items[n]
// the n:th item of the second column
print items[rows + n]
print "\n"
n = n + 1
In your code, you probably have to check for the presence of [rows + i] elements, etc. Also make sure that rounding off odd numbers works as you expect.
source to share