FPDF 页脚获取在 foreach 行上声明的变量

问题描述

我像这样声明了一个名为“$shop”的变量:

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

我想在页脚中使用 $shop。是否有可能 ?我可以使用一个变量使用

global $variable

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

解决方法

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

class my_pdf extends FPDF {
    public $shop;
    public function setshop($input) {$this->shop = $input;}
    function Footer() {
        $this->Cell(15,5,$this->shop,'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);
}