duanlei2150 2016-10-04 12:50
浏览 127
已采纳

PHP While循环只显示最后一行

I'm using TCPDF library to generate PDF of a table .

Table has few number of rows say 10

Among that two rows has same invoice number "78650"

Now i am selecting by invoice number and as desired it should generate PDF with two rows.

But instead it is only fetching the second row that is the last row. That is Serial no 2 is only taken .

Code Below :

<?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');
?>
  • 写回答

3条回答 默认 最新

  • duannue2455 2016-10-04 12:56
    关注

    That's because for iteration you're replacing the content of $html2. You will need to append the content instead.

    So take the blank $html2 variable outside the loop and append the result as you iterate the loop.

    Code would look something like this,

    $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++; 
    
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料