1

A page which retrieves multiple rows from database and is working fine with simple php code. I want to display that page in print/pdf view(using fpdf).

I used FPDF library, but it shows only first row from database. I can't find out the way to solve it out.

<?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();
}

Thanks a lot in advance.

4
  • Why are you using mysql_query()?
    – Xorifelse
    Commented Dec 7, 2016 at 10:29
  • Thanks Xorifelse for dropping me a line. Yeah, it's deprecated. But still it should be worked. And if use mysqli_query() or ideally PDO too, won't resolve the issue, right? Acually I was trying to help someone who is already been coded like this. Commented Dec 7, 2016 at 10:38
  • 1
    Well, debug a little first. Does while($row = mysql_fetch_assoc($result)) {print_r($row);} give you the correct data? If so, you're using FPDF incorrectly. I'm suspecting $pdf->AddPage() returns a page you can write to, so $page = $pdf->AddPage(); $page->Cell(....).
    – Xorifelse
    Commented Dec 8, 2016 at 3:33
  • Yes, you got the point. I have already solve the problem yesterday. Thanks much by the way. Commented Dec 8, 2016 at 4:02

1 Answer 1

1

To solve the issue we have to use FPDF correctly as well as have to start looping after right line of fpdf.

Resolve the issue this way: I just started the looping after

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

Notice that we need that line to create static header row. So, just right after that line we need to start looping:

$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);
}
1
  • Well, I've never used FPDF but I was close. Accept your answer.
    – Xorifelse
    Commented Dec 8, 2016 at 6:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.