0

I am new to codeIgniter, I have a query in my model as
return $query = $this->db->get('users')->result();

and I have passed the result from controller to view as
$data['users'] = $this->admin_login->get_users();

But I am not able to print the passed data properly in my view, because it is in standard class object.

So far I have tried

foreach ($users as $user)
{
    print_r $user;
}

and the result I am getting is

stdClass Object
(
    [user_id] => 1
    [user_name] => mujju
    [email] => [email protected]
    [mobile] => 9021444578
)

stdClass Object
(
    [user_id] => 2
    [user_name] => mujahed
    [email] => [email protected]
    [mobile] => 8989898989
)

stdClass Object
(
    [user_id] => 3
    [user_name] => akas
    [email] => [email protected]
    [mobile] => 6545789878
)

stdClass Object
(
    [user_id] => 4
    [user_name] => sayyed
    [email] => [email protected]
    [mobile] => 654597879686
)

and what I am expecting is to use something like $user['user_id'] , $user['user_name'] etc. in foreach loop.

0

2 Answers 2

3
foreach($users as $user):
    echo 'user id :' . $user->user_id  . '<br>Username : '. $user->user_name. '<br>Email : ' . $user->email . '<br>Mobile : '. $user->mobile;
endforeach;

Simple as that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanx, For quick reply @Birendra.
2

You access a property of an object like this: $user->user_id

Optionally you can pass the data as an array instead of an object to the view:

return $query = $this->db->get('users')->result_array();

Passing the data from the controller to the view works like this:

$this->load->view('your_view', $data);

More information in the user guide

2 Comments

Can I pass data from controller to view as array? How?
@MujahedAKAS added an example ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.