2015-06-17 72 views
0

我是codeigniter和使用會話的新手。我在會話中插入單個數據時遇到問題。獲取數據庫中的信息並插入會話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 
+0

不清楚你想要什麼? –

+0

而不是在你的模型中使用'result_array()',試着使用'row_array()' – Craig

+1

檢查這行代碼'$ this-> users_model-> get_user_info($ query-> acct_type)'' – Saty

回答

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')); 
+0

相同的輸出[電子郵件] => [email protected] [is_logged_in] => 1 [USER_INFO] =>數組 ( [ACCT_ID] => 1 [acct_fname] =>警長 [acct_lname] => Condoriano [acct_mname] => T ) ) – Noobie

+0

答覆已更新。它不能與result_array返回多維數組中的多個記錄和row_array返回單個行相同的輸出。 – kazimt9

+0

我刪除了'user_info'=> $ user_info這一行,使其不是多餘的,謝謝我現在解決的問題[link](http://prntscr.com/7hxqdk) – Noobie

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

做類似下面

$sess_array = array(
        'acct_id' => 1, 
        'email' => [email protected], 
        ..... 
       ); 
$this->session->set_userdata('user_data', $sess_array); 
0

如果你retriving只有一行隨後, 只是改變:

return $query->result_array(); 

return $query->row_array(); 
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->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. 
} 

控制器

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

希望這有助於。^__^

相關問題