Reputation: 11
I can handle virtually anything concerning the usage of FDPF for my project but my challenge now is, is it possible to run foreach loop within FPDF cell? Any suggestion would be appreciated.
$pdf->Cell(47, 8, "$key - $val", 1, 0, 'C');
I am supposed to display the output of $key and $val from foreach loop and result ought to span multline WITHIN the cell. How can I do this if it is possible?
PS: If the foreach run outside the cell, the foreach will keep creating new cell each time which is not what is desired. Thank you all.
Upvotes: 0
Views: 928
Reputation: 21492
There is a special method for multi line cells -- MultiCell
:
This method allows printing text with line breaks. They can be automatic (as soon as the text reaches the right border of the cell) or explicit (via the \n character). As many cells as necessary are output, one below the other.
So you only need to turn the array into a string where the key-value pairs are separated with newline characters, e.g.:
$cell_text = [];
foreach ($values as $key => $val) {
$cell_text[] = "$key - $val";
}
$pdf->MultiCell(47, 8, implode("\n", $cell_text), 1, 'C');
Upvotes: 3
Reputation:
Build the text for the cell in a foreach loop prior to the $pdf->Cell() call. Then display the text i.e.
$tmp="";
foreach(....) {
$tmp .= $key." ".$val."<br\>
}
$pdf->Cell(47, 8, $tmp, 1, 0, 'C');
Upvotes: 0