【问题标题】:PHP While loop displaying only last rowPHP While循环仅显示最后一行
【发布时间】:2017-02-12 15:35:54
【问题描述】:

我正在使用 TCPD 库来生成表格的 PDF。

表格的行数很少,比如 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');
?>

【问题讨论】:

  • 你需要连接$html2变量。

标签: php mysql while-loop tcpdf


【解决方案1】:
$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, '');

【讨论】:

    【解决方案2】:

    第二次提取会覆盖变量 $html2。您可以使用数组 $html2[$i]。

    【讨论】:

      【解决方案3】:

      这是因为对于迭代,您要替换 $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++; 
      
      } 
      

      【讨论】:

      • 太棒了。如果它解决了您的问题,请接受它作为正确答案。
      • 完成了伙计!谢谢
      猜你喜欢
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多