问题
Hello guys I'm making application on the codeigniter framework and I'm trying to echo user info of a specific user but its not really working. The name is not being echoed. Also all the emails of the users in my database are being echoed.
( The link to the right tuser profile works though but the name of the link doesn't. Its a blank link. This is my view file:
foreach($userdetail_list as $row): ?>
<tr>
<td>
<a href="<?php echo base_url() . 'User/userdetails/'.$row['user_id']?>">
<?php echo $row['voornaam']; ?>
</a>
</td>
<td>
<?php echo $row['email'];?>
</td>
</tr>
<?php endforeach; ?>
My model file:
function getdata($user_id){
$this->db->select("*");
$this->db->from('users');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query->result();
}
public function getUserInfo($user_id) {
$arrReturn = array();
$this->db->select('*');
$this->db->from('users');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result)) {
$arrReturn = $result[0];
}
return $arrReturn;
}
My controller file:
public function userdetails($user_id)
{
//load the User_model
$this->load->model('User_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->User_model->getdata($user_id);
$data['user'] = $this->User_model->getUserInfo($user_id);
//laad view
$data['main_content'] = 'profiel_user';
$data['main_content'] = 'details';
$this->load->view('profiel_user',$data);
}
So basically on the view file, I only want 1 email being echoed of 1 specific user with using the user_id and not all the user emails, also I want to see the name on the view page which is not being echoed and its a blank link. name = voornaam
回答1:
Try like this
Model function
// Gets a list of multiple users.
function getdata(){
$query = $this->db->get('users');
return $query->result_array();
}
// Gets the users information
public function getUserInfo($user_id) {
$this->db->where('user_id', $user_id);
$query = $this->db->get('users');
return $query->row_array();
}
Controller multiple users
public function userdetails($user_id) {
$this->load->model('user_model');
$data['userdetail_list'] = $this->user_model->getdata();
$this->load->view('profiel_user', $data);
}
View multiple users list
<?php foreach($userdetail_list as $row){ ?>
<tr>
<td>
<a href="<?php echo base_url('user/userdetails/'.$row['user_id']);?>">
<?php echo $row['voornaam']; ?>
</a>
</td>
<td>
<?php echo $row['email'];?>
</td>
</tr>
<?php } ?>
Or get single user data
public function userdetails($user_id) {
$this->load->model('user_model');
$user_info = $this->user_model->getUserInfo($user_id);
$data['user_id'] = $user_info['user_id'];
$data['username'] = $user_info['username'];
$this->load->view('profiel_user', $data);
}
View
Or for single user option
<a href="<?php echo base_url('user/userdetails/' . $user_id);?>"><?php echo $username;?></a>
来源:https://stackoverflow.com/questions/46560272/echoing-user-information-not-working-in-codeigniter