如何使用PHP和Codeigniter调用类外的数组?

时间:2013-08-28 12:44:15

标签: php fpdf

大家好我刚才需要一些关于访问类外数据数组的帮助我很困惑如何在类外显示变量 这是我的代码:

<?php

date_default_timezone_set('Asia/Manila');

require('resources/fpdf/fpdf.php');

class PDF extends FPDF {

    function Header(){

        //HERE IS THE PLACE WHERE SHOULD I PUT THE ARRAY, BUT I CANT ACCESS IT INSIDE

        $this->SetFont('Arial','B',10);
        $this->Cell(180,5,'PURCHASE ORDER',0,0,'C');
        $this->Ln();
        $this->SetFont('Arial','',9);
        $this->Cell(40,5,'Suppliers Name:'.$data['spname'].'  ');
        $this->Ln();
        $this->SetFont('Arial','',9);
        $this->Ln(20);
    }
}

$query = "SELECT * FROM po_order_details WHERE order_code = '".$code."'"; 
$result = $this->db->query($query);
foreach($result->result_array() as $row){
    $data[] = array($row['item_qty'],  //THIS IS THE ARRAY THAT I NEED TO GET
                    $row['spname'], 
                    $row['spaddress'], 
    );
}
$this->session->set_userdata('session_data',$data);
//Column titles
$pdf = new PDF(); 
$header = array('QTY','ITEM / DESCRIPTION' , 'UNIT PRICE', 'TOTAL AMOUNT'); // CHANGE THIS ALSO
$pdf->SetFillColor(255,0,0);
$pdf->Ln();
$pdf->AddPage();
$pdf->BuildTable($header,$data);
$pdf->Ln();
$pdf->Output();

?>

这就是我希望你能帮助我的一切

2 个答案:

答案 0 :(得分:0)

您可以将其作为参数传递,然后返回结果吗? 像:

function Header($param_array = array()) {
  // ... modifications, bla bla
  return $result_array;
}

然后查找调用Header()的函数,并将该数组参数一直传递给该函数。

当然,这里的缺点是您将修改FPDF类,如果您将来某个时候需要它,将无法正确更新到新版本。

答案 1 :(得分:0)

为什么不直接更改Header方法的原型并将数组作为参数传递给它。

<?php

date_default_timezone_set('Asia/Manila');

require('resources/fpdf/fpdf.php');

class PDF extends FPDF {

    function Header( &$titles, &$data ){

        //HERE IS THE PLACE WHERE SHOULD I PUT THE ARRAY, BUT I CANT ACCESS IT INSIDE
        // use $titles and $data as you like here

        $this->SetFont('Arial','B',10);
        $this->Cell(180,5,'PURCHASE ORDER',0,0,'C');
        $this->Ln();
        $this->SetFont('Arial','',9);
        $this->Cell(40,5,'Suppliers Name:'.$data['spname'].'  ');
        $this->Ln();
        $this->SetFont('Arial','',9);
        $this->Ln(20);
    }
}

$query = "SELECT * FROM po_order_details WHERE order_code = '".$code."'"; 
$result = $this->db->query($query);
foreach($result->result_array() as $row){
    $data[] = array($row['item_qty'],  //THIS IS THE ARRAY THAT I NEED TO GET
                    $row['spname'], 
                    $row['spaddress'], 
    );
}
$this->session->set_userdata('session_data',$data);
//Column titles
$pdf = new PDF(); 

$header = array('QTY','ITEM / DESCRIPTION' , 'UNIT PRICE', 'TOTAL AMOUNT'); // CHANGE THIS ALSO
$pdf->Header( $header, $data );

$pdf->SetFillColor(255,0,0);
$pdf->Ln();
$pdf->AddPage();
$pdf->BuildTable($header,$data);
$pdf->Ln();
$pdf->Output();

?>