douwen7475 2016-07-11 16:42
浏览 62

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

I got some error when I am trying to print my data from my table using DOMPDF in Codeigniter. See my picture click here

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/print-pasien.php
Line Number: 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.

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-07-11 21:04
    关注

    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. If you are using $query->row() the result will be returned as an object.

    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.

    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; to return $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.

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3