siva
siva

Reputation: 1481

ErrorException [ Notice ]: Trying to get property of non-object

controller.php

public function action_user() {
        $user_list = DB::select()->from('users')->execute();
//       print_r($user_list);
        $this->template->content = View::factory('user')
                                    ->bind('user_list',$user_list);
    }

when i print the $user_list in controller like this print_r($user_list); i am getting the values in array.In views i am trying to iterate using for loop and printing he variable.But i am getting this error "ErrorException [ Notice ]: Trying to get property of non-object".If i print the variable as $user_list,it is printing as array.

views.php

 <?php echo $user_list; ?> //printing array
    <?php
        foreach ($user_list as $user):
        echo $user->username;  //getting error here
        endforeach;
    ?>

Upvotes: 0

Views: 2316

Answers (1)

D T
D T

Reputation: 1556

Using the as_object method:

$user_list = DB::select()->from('zid_users')->as_object('User')->execute();

or the ORM

$user_list = ORM::factory('User')->find_all();

Upvotes: 1

Related Questions