【问题标题】:Getting message error PHP: Trying to get property of non-object when trying to print my data in Codeigniter获取消息错误 PHP:尝试在 Codeigniter 中打印我的数据时尝试获取非对象的属性
【发布时间】:2016-07-11 16:42:11
【问题描述】:

当我尝试在 Codeigniter 中使用 DOMPDF 从表中打印数据时出现一些错误。看我的照片click here

遇到 PHP 错误
严重性:通知
消息:试图获取非对象的属性
文件名:views/print-pasien.php
行号:57

以及我的 print-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语句调用它。

【问题讨论】:

  • 我敢打赌,在发布此错误之前,您甚至从未尝试过 Google 那个错误。我说的对吗?
  • 所以$data 不是一个对象。你需要做var_dump($data) 看看它是什么。也许它是一个数组,而不是一个对象。
  • 我试过了,我也尝试在 stackoverflow 上搜索,但我就是不明白,抱歉我还是使用 Codeigniter @AlonEitan 的新手

标签: php codeigniter


【解决方案1】:

在 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...

现在转到您的模型并将 return $query; 更改为 return $query->result(); 并查看现在打印的内容。大概是这样的:

Array
(
    [0] => stdClass Object
        (

有了这个,你现在有一个对象数组。当我碰壁时,我的技巧是首先在模型上 print_r,然后是控制器,最后是视图,以确保我的数据到达以及它是如何组织的,然后再假设一切都完全符合我的预期.

【讨论】:

    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多