如何使用 FPDF 显示多个动态行?

How to show multiple dynamic rows using FPDF?

一个从数据库中检索多行并且使用简单的 php 代码工作正常的页面。我想在 print/pdf 视图中显示该页面(使用 fpdf)。

我使用了 FPDF 库,但它只显示数据库的第一行。我找不到解决的方法。

<?php
session_start();
if($_SESSION['ssn']!="") {
  $search=$_SESSION['search'];
  $status='cash_out';
  include("connection.php");
    require('fpdf/fpdf.php');
    $pdf = new FPDF();
    $pdf->AliasNbPages();
    $pdf->SetFont('Arial', 'B', 18);

    $result= mysql_query("SELECT * FROM transaction_info WHERE cus_id='$search'&&status='$status'");
    while($row = mysql_fetch_assoc($result)) {
        $branch = $row['branch'];
        $date = $row ['date'];
        $cus_pre_bal = $row['cus_pre_bal'];
        $total_bal = $row['cus_total_bal'];
        $trans_bal = $row['cus_trans_bal'];

     $pdf->AddPage();
     $pdf->Cell(0, 20, "Cash Out Report", 1, 1, 'C');
     $pdf->Cell(30, 13, " Date ", 1, 0);
     $pdf->Cell(30, 13, " Branch ", 1, 0);
     $pdf->Cell(42, 13, " Previous Balance ", 1, 0);
     $pdf->Cell(40, 13, " Transaction Bal. ", 1, 0);
     $pdf->Cell(48, 13, " Balance after Trans. ", 1, 1);

     $pdf->Cell(30, 13, "{$date} ", 1, 0);
     $pdf->Cell(30, 13, " {$branch} ", 1, 0);
     $pdf->Cell(42, 13, "      {$cus_pre_bal} ", 1, 0);
     $pdf->Cell(40, 13, "        {$trans_bal} ", 1, 0);
     $pdf->Cell(48, 13, "        {$total_bal} ", 1, 1);
    }
    $pdf->Output();
}

非常感谢。

要解决这个问题,我们必须正确使用 FPDF,并且必须在 fpdf 的右行之后开始循环。

这样解决问题:我刚开始循环

$pdf->Cell(0, 20, "Cash Out Report", 1, 1, 'C');

请注意,我们需要该行来创建静态 header 行。因此,就在该行之后,我们需要开始循环:

$pdf->AddPage();
$pdf->Cell(0, 20, "Cash Out Report", 1, 1, 'C');
$result= mysql_query("SELECT * FROM transaction_info WHERE cus_id='$search'&&status='$status'");
while($row = mysql_fetch_assoc($result)){
  $branch = $row['branch'];
  $date = $row ['date'];
  $cus_pre_bal = $row['cus_pre_bal'];
  $total_bal = $row['cus_total_bal'];
  $trans_bal = $row['cus_trans_bal'];

  $pdf->Cell(30, 13, " Date ", 1, 0);
  $pdf->Cell(30, 13, " Branch ", 1, 0);
}