簡體   English   中英

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 - 支持多列,這是我用來解決我的問題的wat:

$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] 是否存在等。還要確保奇數的四舍五入按照您期望的方式工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM