【发布时间】:2021-01-11 14:39:51
【问题描述】:
我正在尝试在 tFPDF 的标头中显示 mysql 的结果。到目前为止,我已经设法创建了 pdf,但是当我尝试添加要显示的 sql 数据时,我遇到了错误。
我的代码:
...
$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();
}
}
...
错误信息:
Notice: Undefined variable: Total in C:\xampp\...\page.php on line xx
【问题讨论】:
-
只是说 “我遇到错误” 没有帮助,完全没有。 什么错误?
-
@CBroe 我注意到了这一点,我将它添加到你留言的地方。
-
该变量在该方法中不存在。您需要阅读 variable scope - php.net/manual/en/language.variables.scope.php
-
@CBroe 在
function Header中添加了global $Total, $Debt;,我认为它正在工作。