0

I'm trying to figure out I keept getting "trying to get property of non object in my view file. It says its an object in the print r function I ran on the $messages variable but yet it still says that error message.

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>
                    '; } } ?>
4

2 回答 2

4

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

<?php
if (!is_null($messages))
{ 
   foreach ($messages AS $message)
于 2012-04-18T23:40:37.927 回答
0
if (is_array($messages)) {
  foreach ($messages as $message) {
    echo $message->id . "\n";
  }
}
于 2012-04-18T23:46:41.657 回答