2016-10-04 67 views
0

我正在使用TCPDF庫來生成表格的PDF。PHP雖然循環只顯示最後一行

表有極少數的行說,10

其中有兩行具有相同的發票編號「78650」

現在我已經選擇通過發票號並根據需要它應該生成PDF 兩行

但是取而代之的是它只取第二行這是最後一行。這是序號2只是採取。

下面的代碼:

<?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'); 
?> 
+1

您需要連接'$ html2'變量。 –

回答

1

這是因爲迭代您要更換的$html2內容。您需要添加內容。

因此,取循環外的空白$html2變量並在迭代循環時追加結果。

代碼會是這個樣子,

$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++; 

} 
+0

哦,糟糕!感謝隊友..像魅力一樣工作.. –

+0

太棒了。如果它解決了您的問題,請接受它作爲正確答案。 –

+0

完成隊友!謝謝 –

1

第二獲取覆蓋變量$ HTML2。你可以使用數組$ html2 [$ i]。

0
$tbl_header = '<table border="1"><tr> 
     <th>FirstName</th><th>LastName</th><th>Gender</th><th>E-Mail</th><th>Mobile</th><th>DateOfBirth</th><th>Country</th><th>State</th><th>City</th><th>Address</th><th>Zipcode</th><th>File</th></tr>'; 
$tbl_footer = '</table>'; 

$tbl = ''; 
while($row = mysqli_fetch_array($result)) 
    { 
$tbl .= ' 
<tr><td>'.$row['firstname'].'</td><td>'.$row["lastname"].'</td><td>'.$row["sex"].'</td><td>'.$row["email"].'</td><td>'.$row["mobile"].'</td><td>'.$row["dob"].'</td><td>'.$row["country"].'</td><td>'.$row["state"].'</td><td>'.$row["city"].'</td><td>'.$row["address"].'</td> 
<td>'.$row["zipcode"].'</td> 
<td><img src="'.$row["file"].'"/></td> 
</tr> 
'; 

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