X行数后分割表&在While循环中添加文本(使用TCPDF)

时间:2014-01-16 14:15:30

标签: mysql sql tcpdf

我有这样的声明,它产生的行数应该是:

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

如上所述,这将返回正确的数据。但是,我在TCPDF中使用SETXY在页面上设置了一个页脚,并且表格覆盖了这个页面。我需要的是添加文本:

$pdf->AddPage();

在10行之后,表格将在下一页继续。

有什么想法吗?如果无法做到这一点,无论如何将setxy设置为不只是坐在页面的背景中?

1 个答案:

答案 0 :(得分:2)

你只需要一个简单的循环计数器:

$tbl = '';
$counter = 0;

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

编辑:你可能想重构你循环的方式但是现在你已经写了解决方案:

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