X行数后拆分表&在 While 循环中添加文本(使用 TCPDF) [英] Split Table After X Number of Rows & Add Text in While Loop (Using TCPDF)

查看:43
本文介绍了X行数后拆分表&在 While 循环中添加文本(使用 TCPDF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个语句,它产生了它应该做的尽可能多的行:

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

如前所述,这会返回正确的数据.但是,我在 TCPDF 中用 SETXY 设置的页面上有一个页脚,表格对此进行了检查.我需要的是添加文本:

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();

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

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

有什么想法吗?如果这不能做到,有没有办法将 setxy 设置为不只是坐在页面的背景中?

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?

推荐答案

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

You just need a simple looping counter:

$tbl = '';
$counter = 0;

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

您可能想要重构循环方式,但正如您现在编写的那样,解决方案是:

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

这篇关于X行数后拆分表&amp;在 While 循环中添加文本(使用 TCPDF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆