0

I have the following happening in my model:

public function load_user_menu($username)
    {
        $this->db->select('*');
        $this->db->from('menu');
        $this->db->where('username', $username);
        $query = $this->db->get();
        return $query->result();
    }

and the following in my controller:

public function index()
    {     
        /*If user previously logged in and not logged out, username remains in session.
            load username and load profile data. 
        */
        //check if user logged in or not
        if(($this->session->userdata('username')!=""))
        {
            //load data from model
            $profile = array();
            $username = $this->session->userdata('username');
            $result = $this->profileModel->user_profile($username);
             foreach($result as &$value)
              {
                  $profile['userdetails'] = $value;
              }
            $this->load->view('profile', $profile);
        }else{
            //redirect to login function
            $this->login();
        }
    }

but I am getting errors on profile view. I am sure I am accessing them wrong because I am doing this on view:

<? echo $userdetails['profilepic']; ?>

and nothing is showing but this error:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/profile.php

Line Number: 60

which am certain because of the wrong accessing. How can I access the details based on the above?

2
  • Where is $userdetails['profilepic']; in your code? The error says there is nothing to get from that variable, defiantly there isn't anything fetched into $userdetails['profilepic'];. Commented Apr 12, 2013 at 9:57
  • Well what code is on the line that the error mentions? Commented Apr 12, 2013 at 10:05

4 Answers 4

3

assumming you have profilepic inside userdetails and seeing the error you are trying to get he property of nonobject that means

 <? echo $userdetails['profilepic']; ?>

should be

 <? echo $userdetails->profilepic; ?>

since you are taking you result as object and not array

return $query->result();

or you change this to

return $query->result_array();
Sign up to request clarification or add additional context in comments.

Comments

1

$query->result(); produces an object; $query->result_array(); gives you an array

either is fine, depending on your needs

1 Comment

I guess we don't need comments and edits when two people are typing at the same time; happens alot
0

-There is no need of foreach loop in your controller as

$result = $this->profileModel->user_profile($username);
             foreach($result as &$value)
              {
                  $profile['userdetails'] = $value;
              }

-Try this instead.

$profile['userdetails'] = $this->profileModel->user_profile($username);

-To Access in View,use this as

echo $userdetails->profilepic;

Comments

-1

just try this on your view page

<? echo $profilepic; ?>

Comments

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.