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)並且現在一切正常,纔得到一個空數組。再次感謝。 –