Welcome Guest, Not a member yet? Register   Sign In
How to get data objects in controller
#1

I'm really new to this so excuse my query on something that is probably fundamental but...

I have a controller that has the model in the constructor, I have created a method in the model which grabs a record from the database.
I call it in my controller like so:

$data['user'] = $this->jobseekers_model->getUser($userId);

Now I have checked and it is returning an array, I can see it using print_r but I can't seem to figure out how to get an array element, I am trying to set a session var using:

$newdata = array(
        'username'  => $user['username'],
        'email'     => $user['email'],
        'logged_in' => TRUE
        );

 but then I get the error:
Message: Undefined variable: user

can anyone please advise?
Do I need to loop through the array first an assign each array element to a new array? Surely not?
Reply
#2

(This post was last modified: 04-09-2018, 05:42 AM by jreklund.)

You need to pass it to your view.
https://www.codeigniter.com/user_guide/g...views.html

PHP Code:
$this->load->view('user'$data); 

And inside that view:
PHP Code:
<?php echo $user['username'];?>
Reply
#3

Ok thanks I get that, but then how would I get the variables to create the session vars in the controller, because I assume that's where I would need to set them, in the controller not the view?
Reply
#4

(This post was last modified: 04-09-2018, 10:20 AM by jreklund. Edit Reason: Added ; for some reason )

You access multi array like this.
PHP Code:
$newdata = array(
        
'username'  => $data['user']['username'],
        
'email'     => $data['user']['email'],
        
'logged_in' => TRUE
        
); 
Reply
#5

I still can't get it working, I'll try and break the code down into it's simplest parts:

In my model:
// will include the password later
public function jobseekersLogin($email, $password) {

  $this->db->select('*');
  $this->db->from('jobseekers');
  $this->db->where('lc_email', $email);
  $this->db->limit(1);
  $query = $this->db->get();

  if ($query->num_rows() == 1) {
    return $query->result_array();
    } else {
      return false;
      }
  }

in my controller:

// This is returning a large array as tested with print_r
$data['user'] = $this->jobseekers_model->jobseekersLogin($jbsEmail, $jbsPassword);

107: $userId = $data['userData']['lc_id'];

A PHP Error was encountered
Severity: Notice
Message: Undefined index: lc_id
Filename: controllers/Jobseekers.php
Line Number: 107
Backtrace:

lc_id is the primary id column in the database, I would have thought with this code all columns would now be available i.e
$userId = $data['userData']['email];
$userId = $data['userData']['first_name'];

etc etc?
Reply
#6

I have got it working, not entirely sure why but it was the query in the model, I changed it from:

return $query->result_array();

to:

return $query->row_array();

and now it all works, thanks for your help it has helped me nail some other bits that weren't working too.
Reply
#7

(This post was last modified: 04-09-2018, 10:25 AM by jreklund.)

result_array generates a multi array, for when you want users

PHP Code:
$data['user'][0]['username']; // 0 = first user, 1 = second ... 

row_array generates a single array, for when you want a single user

PHP Code:
$data['user']['username']; 
Reply
#8

Got it, nice one, thanks very much.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB