“试图获取非对象的属性”例外?

时间:2012-04-18 23:36:58

标签: php

我正在试图弄清楚我想要“尝试在我的视图文件中获取非对象的属性。它说它是打印r函数中的一个对象我在$ messages变量上运行但是它仍然说错误消息。

Array ( 
[0] => stdClass Object ( 
    [id] => 1 [subject] => Testing Message 1 
    [firstName] => Jeffrey 
    [lastName] => Davidson 
    [dateSent] => April 1st, 2012 
    [messageRead] => 0 )
[1] => stdClass Object ( 
    [id] => 1 [subject] => Testing Message 1 
    [firstName] => Jeffrey 
    [lastName] => Davidson 
    [dateSent] => April 1st, 2012 
    [messageRead] => 0 ) ) 


<?php if (!is_null($messages[0])) { 
                    foreach ($messages[0] AS $message) { 
                        echo '<tr>'; echo '<td style="text-align: center">'; switch ($message->
                    messageRead) { case 0: echo '
                    <img src='.base_url().'assets/img/icons/packs/fugue/16x16/mail.png />
                    '; break; case 1: echo '
                    <img src='.base_url().'assets/img/icons/packs/fugue/16x16/mail-open.png />
                    '; break; case 2: echo '
                    <img src='.base_url().'assets/img/icons/packs/fugue/16x16/mail-reply.png />
                    '; break; case 3: echo '
                    <img src='.base_url().'assets/img/icons/packs/fugue/16x16/mail-forward.png />
                    '; break; } echo '
                    </td>
                    '; echo '
                    <td>
                        '.$message->dateSent.'
                    </td>
                    '; echo '
                    <td>
                        <a href='.base_url().'pmsystem/viewmessage/ '.$message->id.'>'.$message->subject.'</a>
                    </td>
                    '; echo '
                    <td>
                        '.$message->firstName.' '.$message->lastName.'
                    </td>
                    '; echo '
                    <td style="text-align: center">
                        '; $data = array('name' => 'message_id', 'class' => 'messages', 'value' => $message->id); echo form_checkbox($data); echo '
                    </td>
                    '; echo '
                    </tr>
                    '; } } ?>

2 个答案:

答案 0 :(得分:4)

假设$messages是对象数组,则需要在$messages变量上运行foreach,而不是在第一个索引($messages[0])中的对象上运行

<?php
if (!is_null($messages))
{ 
   foreach ($messages AS $message)

答案 1 :(得分:0)

if (is_array($messages)) {
  foreach ($messages as $message) {
    echo $message->id . "\n";
  }
}