Search code examples
phpvariablesforeachfooterfpdf

FPDF Footer get variable declared on foreach line


I declared a variable named "$shop" like this:

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

And I want to use $shop in my footer. Is it possible ? I could use a variable by using

global $variable

But it doesn't work with $shop If you have an answer or seen the answer somewhere, please share it


Solution

  • Create a small function when you extend the fPDF class with your own name. The function does nothing more than set a public variable that holds the value of $shop which you may then use in your footer function.

    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);
    }