سؤال

I'm using TCPDF and Codeigniter. I'm trying to create a custom header with information from my MYSQL-DB: $header_content.

Allthough I can't import the variable into the class MYPDF without getting an error tha the variable doesn't exits.

How do I import the variable into the class?

I'm sure there is an easy solution, I just can't figure it out! :)

controller/pdf.php

class Pdf extends CI_controller { 

    public function __construct() 
    {
        parent::__construct();
        $this->load->model('item_model');
    }


   public function create($nr = 1)
   {
        $data['pd']= $this->item_model->get('personal_details');

        require_once APPPATH.'third_party/tcpdf/tcpdf.php';    

        $this->load->view('create_1', $data);
    }
}

View/create_1.php

    $header_content = $pd->fname.' '.$pd->sname;
    $header_font = $pd->font.;

    class MYPDF extends TCPDF
    {
        function __construct()
        {
            parent::__construct();
        }

        //Page header
        public function Header() 
        {
            // Set font
            $this->SetFont($header_font, '', 10);

            // Title
            $this->Cell(0, 15,  $header_content, 0, false, 'L', 0, '', 0, false, 'M', 'M');
        }
    }

    // create new PDF document
    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
هل كانت مفيدة؟

المحلول

global $header_content ;
$header_content = '123123213dd213';

class MYPDF extends TCPDF
{
    function __construct(  )
    {
        parent::__construct();
    }

    //Page header
    public function Header() {

        global $header_content ;


        // Set font
       $this->SetFont('helvetica', '', 10);

        $this->Cell(0, 15,  $header_content , 0, false, 'L', 0, '', 0, false, 'M', 'M');
    }


}

Much thanks to Merianos Nikos's answer at How to make variable global across entire class

This works just fine. But is it the right way to do it?

نصائح أخرى

class MYPDF extends TCPDF
{
    public $header_content;

    //Page header
    public function Header() 
    {
        // Set font
        $this->SetFont($header_font, '', 10);

        // Title
        $this->Cell(0, 15,  $this->header_content, 0, false, 'L', 0, '', 0, false, 'M', 'M');
    }
}


// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->header_content= $pd->fname.' '.$pd->sname;

It works for me. Maybe you can try use Attributes.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top