JohnDoe122 JohnDoe122 - 9 months ago 66
PHP Question

Using user_id to echo user info on a view page in codeigniter

I'm trying to make a link to a viewpage with using the user_id to display the other user information like for example name and email. This is the link I'm trying to make:

foreach($userdetail_list as $row)
{
?>
<tr>
<td><a href="<?php echo base_url() . 'User/profiel_user/'.$row['user_id']?>">
</tr>


Now I'm trying to gather the user information that belongs to the user_id that I clicked on.
When I click on the link I do see that the link changes to the right user : User/profiel_user/6
But it says 404 Page not found

My User_model file:

<?php


class User_model extends CI_Model {


public function getUserInfoByEmail($email)
{
$q = $this->db->get_where('users', array('email' => $email), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$email.')');
return false;
}
}

public function getUserInfo($user_id)
{
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$user_id.')');
return false;
}
}


public function getdata()
{
$this->db->where('user_id', $id);
$this->db->from('users');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}



}

?>


my full User.php controller file:

<?php

class User extends CI_Controller
{



public function index()
{
$this->load->view('profile', $data);
}



public function __construct()
{
parent::__construct();
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
}


public function userdetails($user_id)
{
//load the User_model
$this->load->model('User_model');

//call function getdata in de Product_model
$data['userdata_list'] = $this->User_model->getdata();

//get product details
$data['user'] = $this->User_model->get_user_info($user_id);

//laad view
$data['main_content'] = 'profiel_user';
$this->load->view('profiel_user',$data);
}



public function profile()
{

$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}

$this->load->view('profile');

}



}


What I have on the view page (profiel_user.php) where the link is linking to:



<div class="container-fluid">
<div class="row">
<div class="col-lg-12 bg-warning" style="font-size:25px">
<center>Gebruikersprofiel</center>
</div>
</div>
</div>



<?php
foreach($userdata_list as $row)
{
?>
<tr>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>













<?php include_once ('templates/header.php'); ?>

<div class="container-fluid">
<div class="row">
<div class="col-lg-12 bg-warning" style="font-size:25px">
<center>Gebruikersprofiel</center>
</div>
</div>
</div>


I hope someone can figure out what I'm doing wrong and why I can not use the user_id to echo or display other user_information.

Database:
table name: users
And user_id is my primary key

Answer Source

Your method name is userdetails not profiel_user so change your href

<td><a href="<?php echo base_url() . 'User/profiel_user/'.$row['user_id']?>">

to

<td><a href="<?php echo base_url() . 'User/userdetails/'.$row['user_id']?>">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download