I'm still new to CI, so bear with me. But I'm having some trouble getting the data to show on user profiles.
Model:
public function user_data()
{
$age = $this->session->userdata('age');
$data = array();
$this->db->select('*');
$this->db->from('users');
$this->db->where('age', $age);
$query = $this->db->get();
return $query->row();
}
Controller:
public function user()
{
$this->load->model('user_model');
$data['row'] = $this->user_model->user_data();
$this->load->view('templates/header');
$this->load->view('pages/profile', $data);
$this->load->view('templates/footer');
}
View:
<h2><?php echo $_SESSION['username'];?>'s Profile</h2>
</br>
<body>
<h4>Age: <?php echo $row['age'];?></h4>
The username shows up, but the age result does not. Also not showing any errors at this point, so I'm a little bit puzzled.
Try echo $this->db->last_query();
in your controller after line
$data['row'] = $this->user_model->user_data();
It will help you to get actual query executing through active record and try the same query through phpmyadmin
you
User row_array
to get the row array from the database.
public function user_data()
{
$age = $this->session->userdata('age');
$this->db->select('*');
$this->db->from('users');
$this->db->where('age', $age);
$query = $this->db->get();
$data = $query->row_array(); //<---- Change this
return $data;
}
Yo Need to load the model first in the controller.
public function user()
{
$this->load->model('user_model');
$row = $this->user_model->user_data();
$this->load->view('templates/header');
$this->load->view('pages/profile', ['row' => $row]); //<--- Try this way
$this->load->view('templates/footer');
}
<h2><?php echo $_SESSION['username'];?>'s Profile</h2>
</br>
<body>
<h4>Age: <?php echo $row['age'];?></h4>
To get your session data you need to do this:
This how you can add data in the session. Example: while creating user login in the login process you can create the user session.
$setSession_data = array(
'username' => $username,
'age' => $age
);
$this->session->set_userdata('your_session_name', $setSession_data);
And this how you can retrieve session data.
$session_data = $this->session->userdata('your_session_name');
print_r($session_data);
echo $session_data['username'];
echo $session_data['age'];
For more detail See this Helper link
Just make sure session library is auto loaded. First check what you get in session.
$this->session->userdata('variable name');
check your query what it is actually executed.
$this->db->last_query();
Finally check out your $row variable
print_r($row);
Have you tried other age values while getting your data from database? The reason age is not printed on your view might be there is no data returned from database. You need to - and we need - see the query executed against the database. So,
$this->db->last_query();
And where do you get the value to set session variable 'username'?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.