Question

I have this statement which produces as many rows as it should do:

$result = mysqli_query($con,"SELECT * FROM b_sale_basket WHERE ORDER_ID=$ID ORDER BY     PRICE * QUANTITY DESC");

$total=0;
while($row = mysqli_fetch_array($result))
{
$quantity = $row['QUANTITY'];
$description = $row['NAME'];
$unitprice = $row['PRICE'];
$lineprice = $row['PRICE']*$row['QUANTITY'];
$total=$total+$lineprice;

$tbl_header = '<table style="width: 650px;" cellspacing="0" cellpadding="5">';
$tbl_footer = '</table>';
$tbl = '';

$tbl .= '
<tr>
    <td style="width: 50px; text-align: left; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.number_format($quantity,0).'</p></td>
    <td style="width: 350px; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.$description.'</p></td>
    <td style="width: 125px; text-align:right; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.number_format($unitprice,2).'</p></td>
    <td style="width: 125px; text-align:right; border-bottom: 0.1em solid #808080;" align="right" ><p style="color:#808080;">'.number_format($lineprice,2).'</p></td>
</tr>
';

 $pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');

As mentioned this returns the correct data. However, I have a footer on the page set with SETXY within TCPDF and the table goes over this. What I need is to add the text:

$pdf->AddPage();

After 10 rows and then the table continues on the next page.

Any ideas? If this can't be done, is there anyway of setting the setxy to not just sit in the background of the page?

Was it helpful?

Solution

You just need a simple looping counter:

$tbl = '';
$counter = 0;

// foreach item in your array...
$counter++;
if ($counter > 9) {
    $pdf->AddPage();
    $counter = 0;
}
$tbl .= '

Edit: you probably want to refactor the way you're looping but as you have it written now the solution is:

$result = mysqli_query($con,"SELECT * FROM b_sale_basket WHERE ORDER_ID=$ID ORDER BY     PRICE * QUANTITY DESC");

$total=0;
$counter = 0;  //implement a counter
while($row = mysqli_fetch_array($result))
{
$counter++; //increment the counter on each loop

$quantity = $row['QUANTITY'];
$description = $row['NAME'];
$unitprice = $row['PRICE'];
$lineprice = $row['PRICE']*$row['QUANTITY'];
$total=$total+$lineprice;

$tbl_header = '<table style="width: 650px;" cellspacing="0" cellpadding="5">';
$tbl_footer = '</table>';
$tbl = '';

$tbl .= '
<tr>
    <td style="width: 50px; text-align: left; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.number_format($quantity,0).'</p></td>
    <td style="width: 350px; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.$description.'</p></td>
    <td style="width: 125px; text-align:right; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.number_format($unitprice,2).'</p></td>
    <td style="width: 125px; text-align:right; border-bottom: 0.1em solid #808080;" align="right" ><p style="color:#808080;">'.number_format($lineprice,2).'</p></td>
</tr>
';

if ($counter > 9) {
    $pdf->AddPage();
    $counter = 0;
}

 $pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top