0

I use Codeigniter to build something. But I have problem when I tried to build module print. I want to use variable that sent from my controller. see my controller script below

public function print_data()
{
    $this->load->library('Pdf');
    $config = $this->config->item('basmalah');
    $data['config'] = $config; //will send data to view
    $this->load->view('/print_laporan_cashdraw',$data);
}

And my view like this

// print_r($config);exit;
$conf = $config;

class MYPDF extends TCPDF {
    //Page header
    public function Header() {

         $this->SetFont('helvetica', '', 12);
         $this->Write(0, 'PT. BASMALAH SIDOGIRI - '.$conf['bas_branch_name'], '', 0, 'C', true, 0, false, false, 0);
         $this->Write(0, $conf['bas_branch_address'], '', 0, 'C', true, 0, false, false, 0);
        // $this->Cell(0, 15, 'PT. BASMALAH SIDOGIRI', 0, false, 'C', 0, '', 0, false, 'M', 'M');
         $style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '0', 'phase' => 10, 'color' => array(0, 0, 0));
         $this->Line(10, 24, 200, 24, $style);
    }
}

i have checked with print_r function its containt data, but i can't access variable

$config

in method Header. What should I do?

Sal00m
  • 2,938
  • 3
  • 22
  • 33

2 Answers2

0
class MYPDF extends TCPDF {
    //Page header
    public function Header() {
         // you have to do this to use global variable.
         global $conf;

         $this->SetFont('helvetica', '', 12);
         $this->Write(0, 'PT. BASMALAH SIDOGIRI - '.$conf['bas_branch_name'], '', 0, 'C', true, 0, false, false, 0);
         $this->Write(0, $conf['bas_branch_address'], '', 0, 'C', true, 0, false, false, 0);
        // $this->Cell(0, 15, 'PT. BASMALAH SIDOGIRI', 0, false, 'C', 0, '', 0, false, 'M', 'M');
         $style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '0', 'phase' => 10, 'color' => array(0, 0, 0));
         $this->Line(10, 24, 200, 24, $style);
    }
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • sorry, i'm newbie in OOP programming. i just want to ask, is it possible to declare variable outside class and can access the variable inside class? – Triyana Syahfrudin Aug 22 '14 at 08:41
  • @TriyanaSyahfrudin When you declare variable outside class, it will be global variable, check the doc how to access global variable. And note, try avoiding global variable. – xdazz Aug 22 '14 at 08:48
0

Use Following code

<?php
    $conf = $config;
    class MYPDF extends TCPDF {
        public $conf = array();
        public function setData($conf){
            $this->conf = $conf;
        }
        //Page header
        public function Header() {
            $this->SetFont('helvetica', '', 12);
            $this->Write(0, 'PT. BASMALAH SIDOGIRI - '.$this->conf['bas_branch_name'], '', 0, 'C', true, 0, false, false, 0);
            $this->Write(0, $this->conf['bas_branch_address'], '', 0, 'C', true, 0, false, false, 0);
            // $this->Cell(0, 15, 'PT. BASMALAH SIDOGIRI', 0, false, 'C', 0, '', 0, false, 'M', 'M');
            $style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '0', 'phase' => 10, 'color' => array(0, 0, 0));
            $this->Line(10, 24, 200, 24, $style);
        }
    }
    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->setData($conf);
    ?>