你好,我是一个初学者在codeigniter框架,我有一些问题。
在我的索引页上,我有登录表单,当用户插入用户名和密码并单击提交按钮时,他调用控制器的方法login,其任务是从表单中收集用户名和密码,并将数据传递给模型方法"login($username, $password)",然后模型方法根据我们是否在数据库中有有效的用户返回true或false,然后控制器的方法要么传递用户进一步或返回他在开始。我的问题是,我想知道用户的访问级别,我做了这个方法模型
public function users_level($username){
$query = $this->db->get_where('users',array('username' => $username) );
$users_level = $query->result(); // I tried with these too $users_level[] = $query->result();
return $users_level;
}
,我想用它返回用户的访问级别,并使用该信息来确定为该特定用户呈现什么视图。
这是来自控制器的登录方法:
public function login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('model_user');
$result = $this->model_user->login($username,$password);
if ($result == true){
$result['level'] = $this->model_user->users_level($username); //i believe that mistake is maybe in this line
$this->welcome(); //I'm going to send information to welcome method about users level of access
}else if ($result == false){
$this->index();
}
}
这是发生的错误
Severity: Warning
Message: Cannot use a scalar value as an array
Filename: controllers/controler_user.php
Line Number: 33
Backtrace:
File: C:'wamp'www'ci_project_2015'application'controllers'controler_user.php
Line: 33
Function: _error_handler
File: C:'wamp'www'ci_project_2015'index.php
Line: 292
Function: require_once
您正在尝试使用$result
作为数组时,它已经初始化为布尔值。您可以使用
$result = array('level' => $this->model_user->users_level($username));
或
$result = array();
$result['level'] = $this->model_user->users_level($username);
然而,这是一个坏主意,因为您使用相同的变量不同的东西。更好的解决方案是重命名其中一个变量,例如
$logged_in = $this->model_user->login($username,$password);
if ($logged_in == true){
或者更好的是,由于布尔值只被使用一次,您可以跳过$logged_in
的初始化,直接在条件
$this->model_user->login($username,$password)
的结果。if ($this->model_user->login($username,$password)) {
您可以省略== true
,因为它返回一个布尔值。