我以前从未在 v2 下的 Codeigniter 中导出到 PDF MySQL。但是当我切换到 v3.1.0 时,在 foreach
中导出到 PDF 时出现问题,出了什么问题?
错误
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: html2pdf/html2pdf.class.php
Controller
public function print_data(){
ob_start();
$class= $this->uri->segment(3);
$data = array(
'get_data' => $this->My_model->detail_id_data($class)
);
$this->load->view('print_detail_data', $data);
$html = ob_get_contents();
ob_end_clean();
require_once('./assets/html2pdf/html2pdf.class.php');
$pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8', 3);
$pdf->WriteHTML($html);
$pdf->Output('id_detail.pdf');
}
Models
function detail_id_data($class) {
$query = $this->db->select('*')
->from('tb_student')
->where('class', $id_data)
->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
}
观看次数
<table style="width: 100%; border: solid 1px black;">
<thead>
<tr>
<th>ID</th>
<th>Name </th>
<th>Class</th>
<th>Study</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
foreach ($get_data as $row){
?>
<tr>
<td><?php $no; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->class; ?></td>
<td><?php echo $row->study; ?>f</td>
</tr>
<?php $no++; } ?>
</tbody>
</table>
最佳答案
在模型中使用row()
而不是result()
..希望它能起作用。
if ($query->num_rows() > 0) {
foreach ($query->row()) as $data) {
$hasil[] = $data;
}
return $hasil;
}
像这样改变你的观点......
<table style="width: 100%; border: solid 1px black;">
<thead>
<tr>
<th>ID</th>
<th>Name </th>
<th>Class</th>
<th>Study</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
foreach ($get_data as $row){
?>
<tr>
<td><?php $no; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['class']; ?></td>
<td><?php echo $row['study']; ?>f</td>
</tr>
<?php $no++; } ?>
</tbody>
</table>
在 Controller 中,
更换它..
$data = array(
'get_data' => $this->My_model->detail_id_data($class)
);
由
$data['get_data'] = $this->My_model->detail_id_data($class);
您是否获得了 $class
的值。请尝试以下操作
echo $class;
关于php - 在codeigniter中使用html2pdf导出pdf时foreach错误数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41168606/