MPDF 无法在 View Code Igniter 中读取我的 PHP Variable/Script

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

我在尝试使用 MPDF 将我的 HTML 转换为 PDF 时遇到问题(在这种情况下,我使用的是 Code Igniter 2.2 版)。 在 view 中,我使用数据库中的 foreachecho 值。下面看看我的代码。

查看文件(home.php)

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

控制器

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");
}

但我的 PDF 输出是这样的。

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

谁能帮我解决这个问题,我将不胜感激。 谢谢

改变

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

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

然后 print_r($home) in home.php 你会得到数组 return.

$jmlKaryawan = $home['jmlKaryawan'];

foreach ($jmlKaryawan as $jml) {

print_r($jml);
}

您没有发送您的数据以供查看

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

会是

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