get information in database and insert into session codeigniter

i am new to codeigniter and using session. i am having a problem in inserting individual data in session.

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->result_array(); }

Controller:

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

this is the row and i want it to put in session individually:

it always appear like this:

get information in database and insert into session codeigniter

how can i make it like this:

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

--------------Solutions-------------

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->row_array(); // changed to row array
}

In Controller

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

Transform this

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

into this

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

do it something like below

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

if you are retriving only one row then , just change :

return $query->result_array();

to

return $query->row_array();

Try this :

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

Hope this helps. ^__^

Category:php Time:2018-12-19 Views:6

Related post

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 4.496 (s). 13 q(s)