Reputation: 85
So I've successfully made the methods in controller so when someone logs in my website and it's the admin ( value 1 in the column admin from db, and the normal users have the value 0), to redirect him to a page who only he can see. Now, I want to display all the users that are not the admin, for the admin, but I don't know how to make it work honestly.
Model:
public function display_users() {
$query = $this->db->query('SELECT id, fname, email, username, password FROM register WHERE admin = 0');
return $query->result();
}
also tried this one in the model:
$this->db->select('id', 'fname', 'lname', 'email', 'username', 'password')
->where('admin', 0)
->get('register')
->result_array();
Controller:
public function admin() {
$isLogged = $this->session->userdata('email');
if ($isLogged) {
$check_admin = $this->signup_model->admin();
if ($check_admin == 1) {
$this->signup_model->display_users();
$this->load->view('navbar');
$this->load->view('header');
$this->load->view('admin');
$this->load->view('footer');
} else {
redirect('users/login');
}
}
}
View:
<table class="usersTable">
<thead>
<tr class="column">
<th class="cell">ID</th>
<th class="cell">First Name</th>
<th class="cell">Last Name</th>
<th class="cell">Email</th>
<th class="cell">Username</th>
<th class="cell">Password</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td class="cell"><?php echo $row->id; ?> </td>
<td class="cell"><?php echo $row->fname; ?> </td>
<td class="cell"><?php echo $row->lname; ?> </td>
<td class="cell"><?php echo $row->email; ?> </td>
<td class="cell"><?php echo $row->username; ?> </td>
<td class="cell"><?php echo $row->password; ?> </td>
</tr>
<?php }} ?>
</tbody>
Upvotes: 0
Views: 1588
Reputation: 3864
Do not use DB functions in your view. You need to set a variable in your controller and send it to your view, something like this :
Model
public function display_users() {
$query = $this->db->query('SELECT id, fname, email, username, password FROM register WHERE admin = 0');
return $query->result_array();
}
Controller
public function admin() {
$return = array();
$isLogged = $this->session->userdata('email');
if ($isLogged) {
$check_admin = $this->signup_model->admin();
if ($check_admin == 1) {
$return['users'] = $this->signup_model->display_users();
$this->load->view('navbar');
$this->load->view('header');
$this->load->view('admin', $return);
$this->load->view('footer');
} else {
redirect('users/login');
}
}
}
View :
<table class="usersTable">
<thead>
<tr class="column">
<th class="cell">ID</th>
<th class="cell">First Name</th>
<th class="cell">Last Name</th>
<th class="cell">Email</th>
<th class="cell">Username</th>
<th class="cell">Password</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user) { ?>
<tr>
<td class="cell"><?php echo $user['id']; ?> </td>
<td class="cell"><?php echo $user['fname']; ?> </td>
<td class="cell"><?php echo $user['lname']; ?> </td>
<td class="cell"><?php echo $user['email']; ?> </td>
<td class="cell"><?php echo $user['username']; ?> </td>
<td class="cell"><?php echo $user['password']; ?> </td>
</tr>
<?php } ?>
</tbody>
Upvotes: 2
Reputation: 26258
Try this:
// Get the role id of logged in user in some variable like $roleId
if($roleId == 1) // Visible only for admin
{
$resultSet = $this->db->query("SELECT id, fname, email, username, password FROM register WHERE role != '1'");
// It will give you the user list other than admin role.
return $resultSet;
}
Controller:
if ($check_admin == 1) { $result = $this->signup_model->display_users();
// pass this variable on the view so that you can use it there.
$this->load->view('navbar');
$this->load->view('header');
$this->load->view('admin', $result); // like that
$this->load->view('footer');
} else {
redirect('users/login');
}
Upvotes: 0