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

时间:2016-12-07 10:27:36

标签: php mysql 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();
}

提前多多感谢。

1 个答案:

答案 0 :(得分:1)

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

以这种方式解决问题:我刚刚开始

之后的循环

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

请注意,我们需要该行来创建静态标题行。所以,就在那一行之后我们需要开始循环:

$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);
}