CodeIgniter显示数据库中的非管理员用户



所以我已经成功地使控制器中的方法,所以当有人登录我的网站,它是admin(值1在列admin从db,正常用户有值0),重定向他到一个页面谁只有他可以看到。现在,我想为admin显示所有非admin的用户,但我不知道如何让它诚实地工作。

模型:

 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>

不要在视图中使用DB函数。你需要在控制器中设置一个变量并将其发送给视图,就像这样:

模型
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>

试试这个:

// 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->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');
    }

相关内容

  • 没有找到相关文章

最新更新