简体   繁体   中英

MPDF Can't Read My PHP Variable/Script in View Code Igniter

I have a problem when trying to convert my HTML into PDF using MPDF (in this case I am using Code Igniter version 2.2). In view , I am using foreach to echo value from database. Here's look at my code.

View File (home.php)

<?php
$indeks = 0;
foreach ($jmlKaryawan as $jml) {
?>

Controller

public function cetak($tahun, $bulan) {
    ini_set('memory_limit', '32M');
    $this->load->model('m_karyawan');
    $data['results'] = $this->m_karyawan->getKaryawan($tahun, $bulan)->result();
    $data['jmlKaryawan'] = $this->m_karyawan->getJumlahKaryawan()->result();
    //load the view and saved it into $html variable
    $html = $this->load->view('home', '', true);
    //this the the PDF filename that user will get to download
    $pdfFilePath = date_default_timezone_get() . ".pdf";
    //load mPDF library
    $this->load->library('m_pdf');
    $pdf = $this->m_pdf->load();
    $pdf->WriteHTML($html);


   $pdf->Output($pdfFilePath, "D");
}

But my PDF output was like this.

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: jmlKaryawan
Filename: views/home.php
Line Number: 43

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/home.php
Line Number: 43

Can anybody help me how to solve it, I will very appreciate that. Thanks

you not send your data to view

 $html = $this->load->view('home', '', true);

It would be

 $html = $this->load->view('home',$data, true);

Change

$html = $this->load->view('home', '', true);

To

$html = $this->load->view('home', $data);

then print_r($home) in home.php you will get the array in return.

$jmlKaryawan = $home['jmlKaryawan'];

foreach ($jmlKaryawan as $jml) {

print_r($jml);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM