werd2u
werd2u

Reputation: 57

Codeigniter, Displaying results from database

I am new to CI, I've been working on this for hours with no success, please help! I am trying to generate user profiles by retrieving information from a database and displaying it in the view (using the USERID). I am getting both an Undefined Variable Error and Trying to Get Property Out of Non-Object error. Here is my code:

Model:

 public function my_data()
{
  $userid = $this->session->userdata('userid');

  $data = array();
  $this->db->select('*');
  $this->db->from('user_profile');
  $this->db->where('userid', $userid);
  $query = $this->db->get();

  return $query->result();
  }

Controller:

    public function profile()
{

         $data['query'] = $this->user_model->my_data();



         $this->load->view('header_loggedin');
        $this->load->view('sidebar_left');
        $this->load->view('user/user_profile', $user);
        $this->load->view('footer');
}

View:

<div class="control-group">
 <i class="icon-user"></i>
            Name: <?php echo $row->name; ?>     
</div>
    <div class="control-group">
 <i class="icon-home"></i>
            Location: <?php echo $data->location;?> 
</div>
        <div class="control-group">
 <i class="icon-briefcase"></i>
            Occupation: <?php echo $data->occupation;?> 
</div>

Thanks so much in advance, I feel like I have tried everything!

Upvotes: 1

Views: 2444

Answers (2)

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17586

here change $user to $data

public function profile()
{

         $data['result'] = $this->user_model->my_data();



         $this->load->view('header_loggedin');
        $this->load->view('sidebar_left');
        $this->load->view('user/user_profile', $data);
        $this->load->view('footer');
}

then if your query returns only one row , you can use

return $query->row_array();

instead of

return $query->result();

in view you can now use

Name: <?php echo $result['name']; ?>
Location: <?php echo $result['location'];?> 
Occupation: <?php echo $result['occupation'];?> 

Upvotes: 0

umefarooq
umefarooq

Reputation: 4574

Hi as new to CI you will face some problems but later you will enjoy it here are some simple stuff you are doing wrong

Model

public function my_data()
{
  $userid = $this->session->userdata('userid');

  $data = array();
  $this->db->select('*');
  $this->db->from('user_profile');
  $this->db->where('userid', $userid);
  $query = $this->db->get();

  //this will return multiple rows or object of arrays
  //return $query->result();
  // you need to send only single row
    return $query->row();
  }

Controller

public function profile()
{
         // in data array key name should be same which you will pass to view    
         $data['row'] = $this->user_model->my_data();



         $this->load->view('header_loggedin');
        $this->load->view('sidebar_left');
        $this->load->view('user/user_profile', $data);
        $this->load->view('footer');
}

View

<div class="control-group">
 <i class="icon-user"></i>
            Name: <?php echo $row->name; ?>     
</div>
    <div class="control-group">
 <i class="icon-home"></i>
            Location: <?php echo $row->location;?> 
</div>
        <div class="control-group">
 <i class="icon-briefcase"></i>
            Occupation: <?php echo $row->occupation;?> 
</div>

for more information please read CI user guide you will understand so many stuff from there CI user guide

Upvotes: 0

Related Questions