Search code examples
phpfpdf

tFPDF - Show mySQL results in header


I am trying to show results of mysql in header in tFPDF. So far, i have manage to create the pdf but when i am trying to add the sql data to display, i am getting error.

My code:

...
$count = "SELECT
             SUM(app_price_out) AS Outcome,
             SUM(app_price_in) AS Income,
             SUM(app_price) AS Turnover
          FROM tblappointment";

foreach($connection->query($count) as $row) {
    $Outcome= $row['Outcome'];
    $Income= $row['Income'];
    $Turnover= $row['Turnover'];
}

$Total = $Income - $Outcome;
$Debt =  $Turnover - $Income;

class PDF extends tFPDF
{
// Page header
function Header()
{
   $this->SetLeftMargin(9);
   $this->AddFont('Calibri','','Calibri.ttf',true);
   $this->SetFont('Calibri','',9);
   $this->Cell(236,12,'Expenses',0,0);
   $this->Cell(18,6,'Total:',0,0,'R');
   $this->Cell(25,6,number_format($Total,0,",","."),0,0,'R');   ===> Error Line at $Total
   $this->Cell(18,6,'Debt:',0,0,'R');
   $this->Cell(25,6,number_format($Debt,0,",","."),0,0,'R');   ===> Error Line at $Debt
   $this->ln();
}
}
...

Error message:

Notice: Undefined variable: Total in C:\xampp\...\page.php on line xx

Solution

  • With the help of CBroe, here is the solution to my problem. Use global.

    The code:

    ...
    function Header()
    {
       global $Total, $Debt;  
       $this->SetLeftMargin(9);
       ...