我想在会话中设置用户数据。我可以在会话数据中设置电子邮件,因为我从登录表单中获取它。 但我不知道如何从数据库获取所有细节并设置为会话。这是我的代码
控制器:
QApplication::postEvent();
模特:
public function login_validation() {
// get form input
$email = $this->input->post("email");
$password = $this->input->post("password");
// form validation
$this->load->library('form_validation');
$this->form_validation->set_rules("email", "Email-ID", "trim|required");
$this->form_validation->set_rules("password", "Password", "trim|required");
if ($this->form_validation->run() == FALSE)
{
// validation fail
$this->load->view('admin/login');
}
else
{
// check for user credentials
$this->load->model('site_model');
$uresult = $this->site_model->get_user($email, $password);
$data["results"]=$this->site_model->getdata_user($email);
foreach ($data as $row) {
$fname = $row['fname'];
}
if (count($uresult) > 0)
{
$data = array(
'email' => $email,
'fname' => $fname,
'lname' => $lname,
'pre'=>$pre,
'is_logged_in' => 1
);
// set session
$this->session->sess_expiration = '0';// will not expire
$this->session->set_userdata($data);
redirect("admin/index");
}
else
{
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Wrong Email-ID or Password!</div>');
$this->load->view('admin/login');
}
}
}
用户表中有一些文件:fname,lname我想设置为session。
提前谢谢。
答案 0 :(得分:0)
查看查询构建器类。 https://www.codeigniter.com/userguide3/database/query_builder.html
使用此资源,您应该能够构建一些读取数据库中列的代码。
如何使用where子句选择列的示例。
$this->db->select('fname, lname');
$this->db->from('users');
$this->db->where('email', $email);
$query = $this->db->get();
答案 1 :(得分:0)
1.)首先,您将从控制器调用您的数据库查询:
$ data = $ this-&gt; Modal_name-&gt; getdata();
2.)模态部分在这里:
$this->db->select('fname, lname');
$this->db->from('users');
$this->db->where('email', $email) ;
$query = $this->db->get();
return $query->result_array();
//通过上面的代码,您将获得所有具有数组格式所需电子邮件ID的条目,该数组将返回到控制器并存储在$ data变量中。
3.)将此变量设置为会话值:
$this->session->set_userdata('all_data', $data);
//在这里,您已将所有返回的值存储在会话中。
4.)您可以访问值的方式: //所有值:
$this->session->userdata('all_data');
//某个特定列的值:
$this->session->userdata['all_data']['column_name'];
//如果要在会话中添加一些特定列:
$this->session->set_userdata('all_data',$data['column_name']);
最重要的是:不要忘记加载会话库,否则无法正常工作
答案 2 :(得分:0)
试试这个
设置模型功能
public function getdata_user($email)
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('email', $email) ;
$query = $this->db->get();
return $query->result();
}
控制器功能中的
public function login_validation() {
// get form input
$email = $this->input->post("email");
$password = $this->input->post("password");
// form validation
$this->load->library('form_validation');
$this->form_validation->set_rules("email", "Email-ID", "trim|required");
$this->form_validation->set_rules("password", "Password", "trim|required");
if ($this->form_validation->run() == FALSE)
{
// validation fail
$this->load->view('admin/login');
}
else
{
// check for user credentials
$this->load->model('site_model');
$uresult = $this->site_model->get_user($email, $password);
$results=$this->site_model->getdata_user($email);
if ($results) {
$sess_array = array();
foreach ($results as $row) {
$sess_array = array(
'email' => $row->email,
'fname' => $row->fname,
'lname' => $row->lname,
'pre' => $row->pre,
'id' => $row->id,
);
$this->session->set_userdata('$sess_array);
}
return TRUE;
redirect("admin/index");
}
else
{
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Wrong Email-ID or Password!</div>');
$this->load->view('admin/login');
}
}
}