![](/img/trans.png)
[英]Getting codeigniter error - A PHP Error was encountered Message: Trying to get property of non-object
[英]Getting message error PHP: Trying to get property of non-object when trying to print my data in Codeigniter
尝试在Codeigniter中使用DOMPDF从表中打印数据时出现错误。 看我的照片点击这里
遇到PHP错误
严重程度:注意
消息:试图获取非对象的属性
文件名:views / print-pasien.php
行号:57
和其他行在我的印刷版视图中也会编号。
好吧,这是我的以下代码:
控制者
public function cetak_pasien(){
$data['pasien'] = $this->a_model->view();
$this->load->view('print-pasien', $data);
// Get output html
$html = $this->output->get_output();
// Load library
$this->load->library('dompdf_gen');
// Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream("print-pasien" . ".pdf", array ('Attachment' => 0));
}
模型
public function view(){
$query = $this->db->query("SELECT kode_pasien,nama_pasien, email_pasien, alamat_pasien, tanggal_lahir,TIMESTAMPDIFF(YEAR,tanggal_lahir,CURDATE()) AS umur, jenis_kelamin, no_telp FROM tb_pasien");
return $query;
}
视图
<table width="100%">
<tr>
<th>No</th>
<th>Kode Pasien</th>
<th>Nama Pasien</th>
<th>Email Pasien</th>
<th>Alamat Pasien</th>
<th>Tanggal Lahir</th>
<th>Umur</th>
<th>JK</th>
<th>No. Telp</th>
</tr>
<?php
if( ! empty($pasien)){
$no = 1;
foreach($pasien as $data){
echo "<tr>";
echo "<td>".$no."</td>";
echo "<td>".$data->kode_pasien."</td>"; //here my problem
echo "<td>".$data->nama_pasien."</td>"; //here my problem
echo "<td>".$data->email_pasien."</td>"; //here my problem
echo "<td>".$data->alamat_pasien."</td>"; //here my problem
echo "<td>".$data->tanggal_lahir."</td>"; //here my problem
echo "<td>".$data->umur."</td>"; //here my problem
echo "<td>".$data->jenis_kelamin."</td>"; //here my problem
echo "<td>".$data->no_telp."</td>"; //here my problem
echo "</tr>";
$no++;
}
}
?>
</table>
PS:我没有在数据库表中保存“ umur”字段的任何数据,我只能通过在模型上使用SQL语句来调用它。
在Codeigniter中,您将分别获得对象数组或纯数组,这取决于您分别使用$ query-> result()还是$ query-> result_array()。 如果使用$ query-> row(),结果将作为对象返回。
我在这里看到的是您没有使用任何这些。 相反,您直接返回$ query ,这将为您提供CI_DB_mysqli_result Object 。
在不调用视图的情况下直接在控制器上尝试以下行:
echo "<pre>";
print_r($data);
echo "</pre>";
我想您会看到类似的内容:
CI_DB_mysqli_result Object
(
[conn_id] => mysqli Object
(
[affected_rows] => 13
[client_info] => 5.5.19...
现在转到您的模型并更改return $ query;。 返回$ query-> result(); 看看现在打印什么。 大概是这样的:
Array
(
[0] => stdClass Object
(
有了这个,您现在有了对象数组。 当我碰壁时,我的诀窍是首先在模型上使用print_r ,然后在控制器上以及最后在视图上,以确保在假设一切都按我的预期进行之前,我的数据已到达并如何组织。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.