0

Ok, In my Codeigniter project I am passing a multidimensional array to my view. The issue I am having is access the data in the array. I use print_r and var_dump to see the array and it is being passed correctly to the view but I am having the hardest time access the data within it! I get this error message, "Trying to access parameter of non-object". Any suggestions?!

Here is the controller: profile.php

    <?php
        class Profile extends CI_Controller {

            public function __construct(){
                parent::__construct();
                $this->load->library('session');

                //Get user data
                $this->load->model('user_model');

            }

            public function user_lookup(){
                //get usering users data
                $email = $this->session->userdata('email');
                //get profile users data
                $username = $this->uri->segment(2,0);

                $user = array(
                            'users' => $this->user_model->getUserData($email),
                            'profile' => $this->user_model->getUserDataWithUsername($username)
                        );

                $this->load->view('profile_view', $user);
            }

        }
    ?>

And here is the view that is recieving the data: profile_view.php

<!DOCTYPE html>
<html>
    <body>
        <?php
             print_r($users[0]);
        ?>
     </body>
 </html>

The output of my print_r statement is:

Array ( [hometown] => Las Vegas [email] => [email protected] [university] => UC Berkley [first_name] => Pete [last_name] => Smith [date] => 1992 ) 1

4
  • if you require echo then use print_r($user,true); and also paste the contents for better answer Commented Mar 20, 2012 at 4:28
  • can you show how your user array looks like? Commented Mar 20, 2012 at 4:29
  • I just added the output in my question and simplified the profile.php page to ignore some of the functions that are irrelevant! I hope this helps you understand the question! Commented Mar 20, 2012 at 4:33
  • to access say hometown then use $user[0]['hometown']; Commented Mar 20, 2012 at 4:37

1 Answer 1

1

Based on your code:

$this->load->view('profile_view', $user);

This mean that you supply array $user as view data. Therefore, on view $user is no longer exists.

You can only use it's element in view:

<?php
    echo $hometown;
    echo $email;
?>

If you want to use $user array in view, use this view load code in controller:

$this->load->view('profile_view', array('user' => $user ));

You can read more about Views in CodeIgniter.

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

2 Comments

Ok I changed the user sub array to users instead of user to differentiate between the parent array and the sub array but I still get the same annoying error...
what line generating that error? it is usually because of $blabla->data: You try to access parameter $data. But, %blabla is not an object.

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.