0

I'm having some trouble getting a SQL query to work inside fpdf, is this even possible?

I currently have

session_start();
require('fpdf.php');    
class PDF extends FPDF
{
    function Header()
        {
            include('config.php');
            $client_check = $db->prepare("SELECT * FROM clients WHERE client_fullname = '".$_SESSION['client_details']."'");
            $client_check->execute();
            while ($row = $client_check->fetch(PDO::FETCH_ASSOC))
            {
                $client_firstname    = $row ['client_firstname'];
                $client_lastname     = $row ['client_lastname'];
                $client_address      = $row ['client_address'];
                $client_jobaddress   = $row ['client_jobaddress'];
                $client_homephone    = $row ['client_homephone'];

                $this->SetFont('Arial', 'B', 12);
                $this->Cell(10,0,'Ph(H):',0,0,'C');
                $this->Cell(20,0,''.$client_homephone.'', 0,0,'C');
                $this->Line(30,61,100,61);
                $this->Cell(210,0,'Job No:',0,0,'C');
                $this->Cell(-120,0,'Model:',0,0,'C');
                $this->Line(110,61,200,61);
            }
        }
    }
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',10);
$pdf->Output();

Everything worked nicely before I added the while loop. Now it just spits out a blank pdf to me. How can I fix this?

2 Answers2

0
$con = mysql_connect("localhost", "{username}", "{password}") or die(mysql_error()) ; 
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db = mysql_select_db("{db}", $con) or die(mysql_error()) ; 
$client= mysql_query("{SELECT * FROM clients}")or die(mysql_error());
while ($client2= mysql_fetch_array($client)){

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',10);

$pdf->Cell(20,0,''.$client2["client_homephone"].'', 0,0,'C');
.....

}

mysql_close($con);

$pdf->Output();
nonononono
  • 33
  • 1
  • 1
  • 6
  • Appreciate the reply but mysql_* functions should not be used. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php –  Mar 14 '13 at 04:33
0

I am afraid you cant do that

This is because your Header function is run every time a new page is added(which is done automatically if a page overflows) to the pdf

Instead you can do like this

  1. Create a constructor to the PDF class which calls a function say Fill_details
  2. Move the while loop into the function Fill_details
  3. Header and Footer functions should contain only that part of the code that is common to all pages say like page borders,footer copyright ,etc
funtime
  • 652
  • 1
  • 5
  • 20