Split Table After X Number of Rows & Add Text in While Loop (Using TCPDF)

南笙酒味 提交于 2019-12-12 01:42:55

问题


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?


回答1:


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, '');


来源:https://stackoverflow.com/questions/21164219/split-table-after-x-number-of-rows-add-text-in-while-loop-using-tcpdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!