TCPDF - PHP foreach in HTML doesn't work

Hello,

I need to create a PDF report (invoice) and I am showing items. description field has usually more that two lines so I need to create it with TCPDF.
So far I understood the TCPDF, but I have a problem to show my items in the html table, they just don’t appear. I think I am doing something wrong with foreach, but I don’t know what, since the code looks right to me. The fields are not empty, since I am testing it with some code in TCPDF code. Can some one please help?

My code in TCPDF:

$pdf->SetFont('times', '', 10);
$pdf->SetXY(20,75);
$pdf->writeHTML($item_number[1], true, false, true, false, '');

$pdf->SetFont('times', '', 10);
$pdf->SetXY(20,80);
$pdf->writeHTML($item_number[2], true, false, true, false, '');

$pdf->SetXY(20,100);
// output items
$pdf->writeHTML($itemshtml, true, false, true, false, '');

My code in onRecord:

//itemsdata
sc_lookup(items,"SELECT item_number, pn, description, qty, price, tax FROM invoice_items WHERE invoice_id='[invoice_id]'");

$itemscount = 2;//{items} -> RecordCount(); doesn't work why????

for ($row = 0; $row < $itemscount; $row++)
{
    $item_number[] = {items[$row][0]};
    $pn[] = {items[$row][1]};
    $description[] = {items[$row][2]};
    $qty[] = {items[$row][3]};
    $price[] = {items[$row][4]};
    $tax[] = {items[$row][5]};
}

// items
$itemshtml = <<<EOD
<table class="GeneratedTable">
<thead>
<tr>
<th>[items_item_number_label]</th>
<th>[items_pn_label]</th>
<th>[items_description_label]</th>
<th>[items_price_label]</th>
<th>[items_price_total_net_label]</th>
</tr>
</thead>
<tbody>
<?php foreach($item_number as $ii){ ?>
    <tr>
    <td>$ii</td>
    <td>$item_number[$ii]</td>
    <td>$description[$ii]</td>
    <td>$qty[$ii]</td>
    <td>$price[$ii]</td>
    <td>$totprice</td>
    <td>tax[$ii]</td>
    </tr>
<?php } ?>
</tbody>
</table>
EOD;

One thing that I see is taht $itemshtml is not assigned using quotes. To start <?php you should have ended the previous one so I’m missing the above ?>

Hello aducom,

thanks for the respond. Unfortunately I don’t understand your answer.

  1. What do you mean by $itemshtml is not assigned using quotes?
    I am using itemshtml in $pdf->writeHTML($itemshtml, true, false, true, false, ‘’);
  2. What do you mean by To start <?php you should have ended the previous one so I’m missing the above ?>
    I am in onRecord where php is used and I create a variable with the html content.

I am really sorry, but I don"t understand your answer.

BR
Christina

$itemshtml = <<<EOD should that not be something like "<<<EOD etc. In onrecord, if you want to start adding other than php code, i.e. javascript then you must finish the php by ?> then add your javascript/html code and restart php by <?php

$itemshtml declared as local in onrecord event, while in Layout PDF->code you use as local also, thus blank

you need to declare it as global variable using [], i think so…