繁体   English   中英

PHP:将数组元素传递给fpdf中的类函数

[英]PHP: passing array elements into class function in fpdf

正在从事项目,需要使用FPDF生成产品详细信息的pdf。 产品详细信息传递到数组中,我需要执行以下操作以将数组'$ prod_details'中的每个可变元素放入类'PDF'中的函数中,如下所示:

我如何尝试传递变量数组元素的示例:

    $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');

我曾尝试运行此脚本,但始终收到错误消息“无法访问空属性” ...

找到下面的代码

<?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
}
?>

希望能得到一些帮助。

您的问题有点含糊。 如果您明确询问要完成的​​工作,这将对您有所帮助。

但我首先看到的是您的fpdf类的子类,您无需编写函数即可完成要使用pdf进行的所有操作。 您只需要扩展您要覆盖(或扩展)的类的各个部分,例如页眉和页脚。

因此扩展它,操纵页眉和页脚,然后关闭类。 创建新的fpdf类的$ pdf实例,然后使用数据处理该对象。 您根本不需要“传递”该数据。

例如:

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

或者,如果这不能实现您想要的功能(尽管我看不到为什么不这样做,但我已经做了很多次了),您始终可以覆盖构造函数。 传入数组(或事件,您创建的自定义对象),并将其存储在私有变量中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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