2016-08-25 85 views
0

所以我成功地在控制器中做的方法,所以当有人登录我的网站,它的管理员(从数据库的列管理员的值为1,并且普通用户有值0),将他重定向到只有他可以看到的页面。现在,我想为管理员显示所有不是管理员的用户,但我不知道如何使其真实工作。CodeIgniter显示用户从数据库不是管理员

型号:

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> 

回答

2

请勿在视图中使用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> 
+0

一个PHP错误遇到 严重性:警告 消息:无效的参数为foreach提供() 文件名:views/admin.php 行号码:17 –

+0

是的,我更新我的答案,模型中的查询是错误的,它应该使用'result_array()'而不是'结果()'。 – roberto06

+0

非常感谢!它现在的作品,我显示错误lname,但其余的工作,我会弄明白,再次感谢您的时间:) –

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; 
} 

控制器:

如果($ check_admin == 1){$ 导致= $这个 - > 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'); 
    } 
+0

我仍然由于我的观点而出现错误: 遇到PHP错误 严重性:注意 消息:试图让非对象的属性,一个PHP错误遇到 严重性:注意 消息:未定义的变量:导致 –

相关问题