所以我已经成功地在控制器中创建了这些方法,所以当有人登录我的网站并且它是管理员(来自db的列admin中的值为1,普通用户的值为0),将他重定向到只有他能看到的页面。现在,我想显示管理员不是管理员的所有用户,但我不知道如何让它真诚地工作。
型号:
public function display_users() {
$query = $this->db->query('SELECT id, fname, email, username, password FROM register WHERE admin = 0');
return $query->result();
}
也在模型中试过这个:
$this->db->select('id', 'fname', 'lname', 'email', 'username', 'password')
->where('admin', 0)
->get('register')
->result_array();
控制器:
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');
}
}
}
查看:
<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>
答案 0 :(得分:2)
请勿在视图中使用数据库功能。您需要在控制器中设置一个变量并将其发送到您的视图,如下所示:
模型
public function display_users() {
$query = $this->db->query('SELECT id, fname, email, username, password FROM register WHERE admin = 0');
return $query->result_array();
}
控制器
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');
}
}
}
查看:
<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>
答案 1 :(得分:0)
试试这个:
// 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;
}
控制器:
if($ check_admin == 1){ $ result = $ this-&gt; signup_model-&gt; 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');
}