首页 > 解决方案 > FPDF 页脚获取在 foreach 行上声明的变量

问题描述

我声明了一个名为“$shop”的变量,如下所示:

foreach($shop_list as $shop)
    function_that_writes_pdf_files($shop);

我想在我的页脚中使用 $shop 。可能吗 ?我可以通过使用来使用变量

global $variable

但它不适用于 $shop 如果您有答案或在某处看到答案,请分享

标签: phpvariablesforeachfooterfpdf

解决方案


当您使用自己的名称扩展 fPDF 类时创建一个小函数。该函数只是设置一个公共变量,该变量保存$shop您可以在页脚函数中使用的值。

class my_pdf extends FPDF {
    public $shop;
    public function setshop($input) {$this->shop = $input;}
    function Footer() {
        $this->Cell(15,5,$this->shop,0,0,'L');  // Using the value of shop here
    }  // end of the Footer function
}

$ourpdf = new my_pdf('P','mm','Letter');
foreach($shop_list as $shop) {
    $ourpdf->setshop($shop); // This sets the value of $shop to the fPDF class you created so the footer function may use it
    function_that_writes_pdf_files($shop);
}

推荐阅读