我是代码点火器和MVC PHP的新手,我已经创建了一个带有在线资源的基本登录系统并且它可以工作,我现在正在努力改进它以便学习......
我的模特:
function validate(){
$this->db->where('Email', $this->input->post('email'));
$this->db->where('Password', sha1($this->input->post('password')));
$query = $this->db->get('admin_users');
if($query->num_rows == 1){
return true;
}
}
function get_admininfo(){
$this->db->where('Email', $this->session->userdata('email'));
$query = $this->db->get('admin_users');
return $query->result();
}
一个用户已经验证我想要提取他们的一些信息并将其存储在会话中,用于通知等虚拟人物。
我的控制器看起来像这样:
function validate_credentials(){
//load the adminship model and then the function validate within it.
$this->load->model('adminship_model');
$query = $this->adminship_model->validate();
// If the admin user successfully validated.
if($query){
$admininfo = $this ->adminship_model->get_admininfo();
$data = array(
'email' => $this->input->post('email'),
'username' => $this->$admininfo['username'],
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('dashboard_controller/dashboard');
}else{
$this->index();
}
}
在仪表板控制器中,我设置了:
$data['email'] = $this->session->userdata('email');
$data['email'] = $this->session->userdata('username');
并将其传递给视图,当我回复$ email时,我收到了电子邮件,但是用户名没有拉我收到此错误:
消息:未定义索引:用户名
这一行:
'username' => $this->$admininfo['username'],
我知道我从模型中以错误的方式提取信息,请有人指出我正确的方向。
答案 0 :(得分:0)
尝试将模型功能更改为:
function get_admininfo(){
$this->db->where('Email', $this->session->userdata('email'));
$query = $this->db->get('admin_users');
if($query->num_rows() == 1){
foreach($query->result() as $row){
$data = $row;
}
return $data;
} else {
return FALSE;
}
}
然后尝试以这种方式获取数据:
$admininfo = $this ->adminship_model->get_admininfo();
$data = array(
'email' => $this->input->post('email'),
'username' => $admininfo->username,
'is_logged_in' => true
);
修改强>
尝试获取all_userdata()
,因为您打算使用它:
$data['userdata'] = $this->session->all_userdata(); // returns an associative array
然后将其传递给视图并使用$userdata['username']
在视图中将其显示出来,这通常适用于我。
方法强>
如果上面的代码不起作用,可能最简单的方法是指定列的名称,如:
function get_admininfo(){
$this->db->where('Email', $this->session->userdata('email'));
$query = $this->db->get('admin_users');
if($query->num_rows() == 1){
$row = $query->row();
return $row->username; //assuming this is the name of the column
} else {
return FALSE;
}
}
将控制器代码更改为:
$admininfo = $this ->adminship_model->get_admininfo();
$data = array(
'email' => $this->input->post('email'),
'username' => $admininfo,
'is_logged_in' => true
);
答案 1 :(得分:0)
功能
function get_admininfo(){
$this->db->where('Email', $this->session->userdata('email'));
$query = $this->db->get('admin_users');
return $query->result();
}
正在尝试将查询作为对象返回
和
$admininfo = $this ->adminship_model->get_admininfo();
$data = array(
'email' => $this->input->post('email'),
'username' => $this->$admininfo['username'],
'is_logged_in' => true
);
正试图将$ data ['username']设置为$ this-> admininfo的预期数组值,但它返回了一个对象
尝试改变:
return $query->result();
到
return $query->row_array();