收到消息错误 PHP:尝试在 Codeigniter 中打印数据时尝试获取非对象的属性


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

以及我的打印-pasien 视图上的其他行数。

好吧,这是我的以下代码:

控制器

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对象

尝试在控制器上直接打印以下行,而无需调用视图:

echo "<pre>";
print_r($data);
echo "</pre>";

我想你会看到类似的东西:

CI_DB_mysqli_result Object
(
    [conn_id] => mysqli Object
        (
            [affected_rows] => 13
            [client_info] => 5.5.19...

现在转到您的模型并更改返回$query;返回$query->result();并查看现在打印的内容。 可能是这样的:

Array
(
    [0] => stdClass Object
        (

有了这个,你现在有一个对象数组。 当我碰壁时,我的诀窍是首先print_r模型,然后是控制器,最后是视图,以确保我的数据到达以及它的组织方式,然后再假设一切都完全按照我的预期进行。