简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM