简体   繁体   中英

PHP: passing array elements into class function in fpdf

Working on a project and need to generate pdf of the product details using FPDF. The product details are passed into an array and I need the following to get each of the variable elements in the array '$prod_details' into the functions within the class 'PDF' as shown below:

Examples of how I tried passing the variable array elements:

    $this->Cell(30,8,$prod_data['prod_name'],0,0,'C');
    $this->Cell(30,10,$prod_data['company_name']);
    $this->Cell(20,8,$prod_data['prod_cost'],0,0,'C');

I have tried running this script but I keep getting an error message 'Cannot access empty property'...

find the codes below

<?php

@include_once("includes/db.php");
require('fpdf/fpdf.php');
@include_once("includes/class_product_info.php");

$obj = new allProducts();
$prod_data = array();
if(isset($_GET['c_id'])){
        $prod_data = $obj->getProdDetails($_GET['c_id']);

class PDF extends FPDF
{   


public $prod_data;

public function createData($input){
    $this->prod_data = $input;
}

function Header()
{
    // Logo
    $this->Image('big_logo.png',10,6,30);
    // Arial bold 15
    $this->SetFont('Arial','B',20);
    // Move to the right
    $this->Cell(40);    
    // Title
    $this->Cell(30,10,$this->prod_data['company_name']);
    // Draw an header line
    $this->Line(10,26,200,26);
    // Line break
    $this->Ln(20);
}

function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);

    // Begin with regular font
    $this->SetFont('Arial','',9);
    // Then put a blue underlined link
    //$this->SetTextColor(0,0,255);
    $this->SetFont('','U');
    $this->Write(10,$this->prod_data['company_name'],'http://www.skills.com');
    // Arial italic 8
    $this->SetFont('Arial','I',9);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().' ',0,0,'R');
}

function prodDetailTop()
{
// Course title cell
$this->Cell('',10,'',0,0,'C');
$this->Ln();

/* Build cells for the first row */

$this->SetFont('Arial','',10);
$this->SetY(40);

// First Row
$this->Cell(35,8,'Product Name : ',0,0,'L');
$this->Cell(30,8,$this->prod_data['prod_name'],0,0,'C');
$this->SetX(150);
$this->Cell(25,8,'Product Cost : ',0,0,'L');
$this->Cell(20,8,$this->prod_data['prod_cost'],0,0,'C');
$this->Ln();

// Second Row
$this->Cell(35,8,'Discount : ',0,0,'L');
$this->Cell(30,8,$this->prod_data['disc_amt'],0,0,'L');
$this->SetX(150);
$this->Cell(25,8,'No Purchased : ',0,0,'L');
$this->Cell(20,8,$this->prod_data['items_count'].' product(s)',0,0,'L');
$this->Ln();
}


function prodDetailBtm()
{

$this->SetY(80);

$this->Write(10,$this->prod_data['prod_desc']);

}

function generatePageData()
{
    $this->AddPage();
    $this->prodDetailTop();
    $this->prodDetailBtm();
}
}

$pdf = new PDF();
$pdf->createData($prod_data);
//$pdf->Header();

$pdf->generatePageData();
$pdf->Output();

}
else {

?>
    <script language="javascript">
    window.location = "prod_invoice_err.php";
    </script>
<?php
}
?>

Hope to get some help.

Your question is a little vague. It would be helpful it you asked specifically what you're trying to accomplish.

But the first thing I see is that your subclass of the fpdf class, you don't need to write functions to do each and everything you want to do with the pdf. You only need to extend the parts of the class you are overriding (or extending), like header and footer.

So extend it, manipulate header and footer, then close the class. Create your $pdf instance of your new fpdf class, then manipulate that object with your data. You don't need to 'pass in' that data at all.

for instance:

$pdf->Ln(10);
$pdf->Cell($w,9,$title,1,1,'C',true); //from fpdf tutorial

Or, if that doesn't accomplish what you want (although I can't see why it wouldn't, I've done this lots of times), you can always override the constructor. Pass in an array (or event a custom object that you create), and store that in a private variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM