TCPDF -循环数据按两列布局


TCPDF - loop data in two columns layout

我使用TCPDF,目前我使用array_chunk在两列中列出数据,这很好。但是我需要数据显示在第一列,然后第二列,见下面:

Currently:
    1   2
    3   4
    5   6
    7   8
    9   10
Should be:
    1   6
    2   7
    3   8
    4   9
    5   10

这是代码:

<?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>

我的第二个查询(复杂),如果有超过30行我需要能够使用$pdf->AddPage();

TCPDF -支持多列,这是我用来解决我的问题:

$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()

代码将添加自动分页符并继续到下一页。

我有一段时间没有使用PHP了,所以我将让您编写代码,但希望这将帮助您解决问题。

我认为你第二个问题是最简单的一个:你每页只能有30行。由于每行有2个条目,这意味着每页有60个条目。因此,只需将数组拆分为数组,每个数组包含60个元素,就像这样,在伪代码中:

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

第二个问题是:您希望按列创建输出列,但是HTML要求您逐行输出。因此,在输出该行之前,我们必须知道总共要输出多少行:

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

在你的代码中,你可能需要检查items[rows + i]是否存在等等。还要确保奇数的四舍五入按您期望的方式工作。