[英]Getting message error PHP: Trying to get property of non-object when trying to print my data in Codeigniter
I got some error when I am trying to print my data from my table using DOMPDF in Codeigniter. 尝试在Codeigniter中使用DOMPDF从表中打印数据时出现错误。 See my picture click here 看我的照片点击这里
A PHP Error was encountered 遇到PHP错误
Severity: Notice 严重程度:注意
Message: Trying to get property of non-object 消息:试图获取非对象的属性
Filename: views/print-pasien.php 文件名:views / print-pasien.php
Line Number: 57 行号:57
and other lines number on my print-pasien views as well. 和其他行在我的印刷版视图中也会编号。
Well, this is my following code : 好吧,这是我的以下代码:
Controller 控制者
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));
}
Model 模型
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;
}
View 视图
<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: I didnt hold any data of 'umur' field on my database table, I only call it by using SQL statement on my model. PS:我没有在数据库表中保存“ umur”字段的任何数据,我只能通过在模型上使用SQL语句来调用它。
In Codeigniter you will get an array of objects or a pure array depending if you are using $query->result() or $query->result_array() respectively. 在Codeigniter中,您将分别获得对象数组或纯数组,这取决于您分别使用$ query-> result()还是$ query-> result_array()。 If you are using $query->row() the result will be returned as an object. 如果使用$ query-> row(),结果将作为对象返回。
What I see here is that you are not using any of these. 我在这里看到的是您没有使用任何这些。 Instead you are returning $query directly which will give you a CI_DB_mysqli_result Object . 相反,您直接返回$ query ,这将为您提供CI_DB_mysqli_result Object 。
Try these lines on your controller printing directly without calling your view: 在不调用视图的情况下直接在控制器上尝试以下行:
echo "<pre>";
print_r($data);
echo "</pre>";
I think you'll see something like: 我想您会看到类似的内容:
CI_DB_mysqli_result Object
(
[conn_id] => mysqli Object
(
[affected_rows] => 13
[client_info] => 5.5.19...
Now go to your model and change return $query; 现在转到您的模型并更改return $ query;。 to return $query->result(); 返回$ query-> result(); and see what prints now. 看看现在打印什么。 Probably something like: 大概是这样的:
Array
(
[0] => stdClass Object
(
With this you now have an Array of objects. 有了这个,您现在有了对象数组。 My trick when I hit a wall is to first print_r on the model, then the controller and finally on the view just to make sure my data arrived and how it is organised before assuming everything came exactly as I expected. 当我碰壁时,我的诀窍是首先在模型上使用print_r ,然后在控制器上以及最后在视图上,以确保在假设一切都按我的预期进行之前,我的数据已到达并如何组织。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.