1

hello guys i just need a little help here about accessing a data array outside a class i'm so confused on how to show the variable outside the class Here's my code below:

<?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();

?>

that's all i hope you can help me

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • try with `$this->session->all_userdata()` – Janith Chinthana Aug 28 '13 at 12:47
  • possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – deceze Aug 28 '13 at 12:51
  • i just try this but it doesn't working any other idea on how to call a variable outside the clas – Samsung SungSam Aug 28 '13 at 12:58
  • Guess: $this->Cell(40,5,'Suppliers Name:'.$this->data['spname'].' '); – Cups Aug 28 '13 at 13:02
  • i have tried it already but it doesn't working. $this->Cell(40,5,'Suppliers Name:'.$this->data['spname'].' '); – Samsung SungSam Aug 28 '13 at 13:08

2 Answers2

0

Could you just pass it as a parameter and then return the result? Like:

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

And then look up the function which calls that Header() and pass that array parameter all the way through that function.

Of course, the drawback here is that you would modify FPDF class and would not be able to properly update to an newer version, if you need it at some point in the future.

0

Why not just change the prototype of your Header method and pass the array as a parameter like this.

<?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();

?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • i have encounter a problem i can't solve Missing argument 1 for PDF::Header() Missing argument 2 for PDF::Header() error what am i suppose to do? any help? thanks in advance – Samsung SungSam Aug 29 '13 at 09:43