0
使用FPDF生成项目并需要生成产品详细信息的pdf。产品详细信息被传递到一个数组中,我需要以下内容来获取数组'$ prod_details'中的每个可变元素到'PDF'类中的函数中,如下所示:PHP:将数组元素传递给fpdf中的类函数
示例我如何尝试传递变量数组元素:
$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
}
?>
代码
希望得到一些帮助。
感谢您抽空帮忙。我已经重新编辑了这个问题,现在更具体。我希望你能够在阅读完代码之后明白我正在阅读的代码所带来的挑战。谢谢! –
好的。首先,你正在调用setData,但是你还没有定义这个函数。看起来你称它为createData。其次,你有一些命名冲突。如果你想访问你的$ prod_data的classes属性,你应该使用$ this-> prod_data ['whatever_key']。因此,请尝试将方法调用更改为createData,对数组执行var_dump以确保它实际上包含您尝试使用的数据,然后将您的变量引用更改为$ this-> prod_data而不是$ prod_data。 – Ryan
非常感谢您的帮助。我更正了方法名称以及我如何访问类属性,但是直到我意识到我没有包含我的数据库连接脚本(db.php)并且现在一切正常,才得到一个空数组。再次感谢。 –