問題描述
我正在使用 TCPD 庫來生成表格的 PDF.
I'm using TCPDF library to generate PDF of a table .
表的行數很少,比如 10
Table has few number of rows say 10
其中兩行發票編號相同78650"
現在我按發票編號選擇,并根據需要生成帶有兩行的 PDF.
Now i am selecting by invoice number and as desired it should generate PDF with two rows.
但它只獲取最后一行的第二行.那是 Serial no 2 只被拿走了.
But instead it is only fetching the second row that is the last row. That is Serial no 2 is only taken .
代碼如下:
<?php
require_once('TCPDF/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 9);
$pdf->AddPage();
$html1 = '<table cellspacing="0" class="table table-striped">
<tr>
<th>SL No.</th>
<th>Product</th>
<th>Description</th>
<th>Qty</th>
<th>Total</th>
</tr>';
$sql = ("select * from invoice WHERE invoice_no = 78650 ORDER BY invoice_id ASC");
$result=mysqli_query($connection,$sql);
$i = 1;
while($row = mysqli_fetch_array($result)){
$pr = $row['product'];
$dr = $row['description'];
$qty = $row['qty'];
$total = $row['total'];
$html2 = "<tr>
<td>".$i."</td>
<td>".$pr."</td>
<td>".$dr."</td>
<td>".$qty."</td>
<td>".$total."</td>
</tr>";
$i++; }
$sql_1 = ("select *,SUM(total)as tot from invoice WHERE invoice_no = 78650 GROUP BY invoice_no");
$result_1=mysqli_query($connection,$sql_1);
$row_1 = mysqli_fetch_array($result_1);
$tot = $row_1['tot'];
$html3 = "<tr>
<td></td>
<td></td>
<td></td>
<td>Total: </td>
<td>".$tot."</td>
</tr>
</table>";
$html = $html1.$html2.$html3;
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
$pdf->Output('htmlout.pdf', 'I');
?>
推薦答案
那是因為對于迭代,您正在替換 $html2
的內容.您需要改為附加內容.
That's because for iteration you're replacing the content of $html2
. You will need to append the content instead.
因此,將空白的 $html2
變量移出循環并在迭代循環時附加結果.
So take the blank $html2
variable outside the loop and append the result as you iterate the loop.
代碼看起來像這樣,
$html2="";
while($row = mysqli_fetch_array($result)){
$pr = $row['product'];
$dr = $row['description'];
$qty = $row['qty'];
$total = $row['total'];
$html2 = $html2."<tr>
<td>".$i."</td>
<td>".$pr."</td>
<td>".$dr."</td>
<td>".$qty."</td>
<td>".$total."</td>
</tr>";
$i++;
}
這篇關于PHP While 循環只顯示最后一行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!