CodeIgniter中使用FPDF生成PDF时数据无法传递显示的问题
我一眼就看出问题所在了——你的FPDF类方法根本拿不到控制器传递过来的$result数据!
在CodeIgniter中,虽然你把$data['result']传给了视图,但视图里的printview类内部的方法(比如viewdata())是无法直接访问这个全局变量的,因为类方法的作用域和视图的全局作用域是分开的。这就是为什么表头能显示,但数据出不来的核心原因。
下面给你两种可行的解决方案,选一种你觉得顺手的就行:
方案一:通过构造函数传递数据
修改你的PDF视图类,把$result通过构造函数传入并存储为类属性,这样类里的方法就能直接使用了:
class Printview extends FPDF { private $result; // 定义私有属性存储数据 // 重写构造函数,接收$result参数 function __construct($result) { parent::__construct(); // 调用父类构造函数 $this->result = $result; // 把数据保存到类属性中 } function Header() { $this->SetFont('Arial','B',15); $this->Cell(276,5,"VendorDetails",0,0,'C'); $this->ln(15); $this->SetFont('Arial','B',10); $this->Cell(55,10,"Name",1,0,'C'); $this->Cell(90,10,"Address",1,0,'C'); $this->Cell(50,10,"Telephone",1,0,'C'); $this->Cell(70,10,"Email",1,0,'C'); $this->ln(); } function Footer(){ $this->SetY(-15); $this->SetFont('Arial','I',8); $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); } function viewdata(){ // 直接使用类属性$this->result if(!empty($this->result)){ foreach($this->result as $values){ $this->SetFont('Arial','',10); $this->Cell(55,10,$values['vndr_name'],1,0,'L'); $this->Cell(90,10,$values['vndr_address'].','.$values['state'].'-'.$values['vndr_pincode'],1,0,'L'); $this->Cell(50,10,$values['vndr_telephone'].','.$values['vndr_mobile'],1,0,'L'); $this->Cell(70,10,$values['vndr_mailid'],1,0,'L'); $this->ln(); } } } } // 实例化时把$result传递给构造函数 $this->pdf = new Printview($result); $this->pdf->SetMargins(15, 10, 20); $this->pdf->AliasNbPages(); $this->pdf->AddPage('L','A4',0); $this->pdf->viewdata(); $this->pdf->Output();
方案二:调用viewdata时直接传参
这种更简单,修改viewdata()方法让它接收$result参数,调用的时候直接传进去:
class Printview extends FPDF { function Header() { // 表头代码保持不变 $this->SetFont('Arial','B',15); $this->Cell(276,5,"VendorDetails",0,0,'C'); $this->ln(15); $this->SetFont('Arial','B',10); $this->Cell(55,10,"Name",1,0,'C'); $this->Cell(90,10,"Address",1,0,'C'); $this->Cell(50,10,"Telephone",1,0,'C'); $this->Cell(70,10,"Email",1,0,'C'); $this->ln(); } function Footer(){ // 页脚代码保持不变 $this->SetY(-15); $this->SetFont('Arial','I',8); $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); } // 修改viewdata方法,接收$result作为参数 function viewdata($result){ if(!empty($result)){ foreach($result as $values){ $this->SetFont('Arial','',10); $this->Cell(55,10,$values['vndr_name'],1,0,'L'); $this->Cell(90,10,$values['vndr_address'].','.$values['state'].'-'.$values['vndr_pincode'],1,0,'L'); $this->Cell(50,10,$values['vndr_telephone'].','.$values['vndr_mobile'],1,0,'L'); $this->Cell(70,10,$values['vndr_mailid'],1,0,'L'); $this->ln(); } } } } $this->pdf = new Printview(); $this->pdf->SetMargins(15, 10, 20); $this->pdf->AliasNbPages(); $this->pdf->AddPage('L','A4',0); $this->pdf->viewdata($result); // 调用时传入$result数据 $this->pdf->Output();
额外检查点
- 先确认控制器里的
$data['result']确实有数据:可以在控制器printpage()方法里加一行var_dump($data['result']); exit;,看看是不是真的从数据库拿到了数据。 - 检查模型返回的字段是否正确:比如
state字段是不是真的被查询出来了,避免拼写错误。 - 类名大小写统一:你之前定义的是
printview,但实例化用的是Printview,PHP虽然不敏感,但最好保持一致,避免奇怪的问题。
内容的提问来源于stack exchange,提问作者itsme
相关产品推荐
相关产品推荐

