获取数据库中的信息并插入到会话codeigniter中

时间:2015-06-17 07:39:19

标签: php database codeigniter session

我是codeigniter和使用会话的新手。我在会话中插入个人数据时遇到问题。

型号:

function get_user_info() {
    $user_email = $this->input->post('signin-email');
    $this->db->select('acct_id, acct_fname, acct_lname, acct_mname');
    $this->db->where('email', $user_email);
    $query = $this->db->get('account');

    return $query->result_array();
}

控制器:

public function LoginValidation(){
    $this->load->library('form_validation');

    $this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|callback_validate_credentials');
    $this->form_validation->set_rules('password', 'Password', 'required|trim|md5');
    $this->load->model('users_model');
    if($this->form_validation->run()){
        $data = array(
            'email' => $this->input->post('signin-email'),
            'is_logged_in' => 1,
            'user_info' => $this->users_model->get_user_info($query->acct_type)
        );
        $this->session->set_userdata($data);

        redirect('home');
    } 
}

这是行,我希望它单独进行会话:

enter image description here

总是这样:

enter image description here

我怎么能这样做:

[acct_id] => 1
[acct_fname] => Sgt
[acct_lname] => Condoriano
[acct_mname] => T

5 个答案:

答案 0 :(得分:0)

转换此

function get_user_info() {
    $user_email = $this->input->post('signin-email');
    $this->db->select('acct_id, acct_fname, acct_lname, acct_mname');
    $this->db->where('email', $user_email);
    $query = $this->db->get('account');

    return $query->result_array();
}

进入这个

function get_user_info() {
    $user_email = $this->input->post('signin-email');
    $this->db->select('acct_id, acct_fname, acct_lname, acct_mname');
    $this->db->where('email', $user_email);
    $query = $this->db->get('account');

    $result = $query->result_array();
    return reset($result);
}

答案 1 :(得分:0)

模型

function get_user_info() {
    $user_email = $this->input->post('signin-email');
    $this->db->select('acct_id, acct_fname, acct_lname, acct_mname');
    $this->db->where('email', $user_email);
    $query = $this->db->get('account');

    return $query->row_array(); // changed to row array
}

在控制器

$user_info = $this->users_model->get_user_info(); // method does not have any arg
$data = array(
            'email' => $this->input->post('signin-email'),
            'is_logged_in' => 1,
            'user_info' => $user_info
        );
$this->session->set_userdata($data);
/* try this for adding as direct index */
$this->session->set_userdata($user_info);
var_dump( this->session->userdata('acct_id') );
var_dump( this->session->userdata('acct_fname') );
var_dump( this->session->userdata('acct_lname') );
var_dump( this->session->userdata('acct_mname') );

答案 2 :(得分:0)

做类似下面的事情

$sess_array = array(
                    'acct_id' => 1,
                    'email' => qwe@qwe.com,
                    .....
                );
$this->session->set_userdata('user_data', $sess_array);

答案 3 :(得分:0)

如果你只追回一行,那么, 只是改变:

return $query->result_array();

return $query->row_array();

答案 4 :(得分:0)

试试这个:

<强> MODEL

function get_user_info() {
    $user_email = $this->input->post('signin-email');
    $this->db->select('acct_id, acct_fname, acct_lname, acct_mname');
    $this->db->where('email', $user_email);
    $query = $this->db->get('account');

    return $query->first_row('array'); //this is use when you only want to retrieve a single user and use result_array() for more users to be retrieve.
}

<强> CONTROLLER

public function LoginValidation(){
    $this->load->library('form_validation');
    $this->load->helper(array('form','url')); //load if not yet loaded in autoload.php

    $this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|callback_validate_credentials');
    $this->form_validation->set_rules('password', 'Password', 'required|trim|md5');
    if($this->form_validation->run() == false){
       $this->load->view('path'); //page you want to be view if validation is false
    }else {
      $input = $this->input->post();
      $user_info = $this->users_model->get_user_info();
      $data = array(
            'email' => $input['signin-email'],
            'is_logged_in' => 1,
            'user_info' => $user_info
        );
        $this->load->model('users_model');
        $this->session->set_userdata($data);

        redirect('home');
    } 
}

希望这会有所帮助。 ^ __ ^