nik
nik

Reputation: 69

How to set an array in session in codeigniter?

I am little bit struggling in setting array in session.

Here is my code:-

Controller:

function taketest(){
    $this->load->model('', '');
    $questions_for_test = $this->session->userdata('questions_for_test');
    $difficulty_level = $this->session->userdata('difficulty_level');
    $question_id = $this->session->userdata('question_id');
    if($question_id==""){
        $question_id = $this->myjcat->returnRandomQuestion($questions_for_test,$difficulty_level);
        $this->session->set_userdata('question_id',$question_id);
    }
    $question_details = $this->myjcat->getQuestion($question_id);
}

Model:

function returnRandomQuestion($questions_for_test, $difficulty_level){
    $question_id = array_rand($questions_for_test[$difficulty_level], 1);
    $used_question=array();
    $used_questions=$questions_for_test[$difficulty_level][$question_id];
    $this->session->set_userdata('used_questions',$used_questions);
    return $questions_for_test[$difficulty_level][$question_id];
}

But when I call:

$used_questions = $this->session->userdata('used_questions');

in controller in the controller it will not return me an array.It gives me last value stored in it.

Upvotes: 2

Views: 12094

Answers (4)

Sushil Kumar Singh
Sushil Kumar Singh

Reputation: 129

You can set array values in session data like this :-

$this->session->set_userdata('used_questions', json_encode($used_questions));

And retrieve the data as :-

json_decode($this->session->userdata('used_questions'));

If you want the retrieved array data as associative array :-

json_decode($this->session->userdata('used_questions'), true);

Hope it hepls you :)

Upvotes: 2

Nikko R.
Nikko R.

Reputation: 178

This is because the data you pass to the $used_questions is a value.

You might want to do something like this:

array_push($used_questions, $questions_for_test[$difficulty_level][$question_id]);

*appending / adding a new value to an array

Upvotes: 0

cwallenpoole
cwallenpoole

Reputation: 81988

I could be misreading things, but it looks like you are only storing one value.

// this code:
$used_questions=$questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);

// is the same as this code
$this->session->set_userdata('used_questions',$questions_for_test[$difficulty_level][$question_id]);

You're probably looking for this:

// fetch the stored copy first.
$used_questions = $this->session->userdata('used_questions');
if(!is_array($used_questions)) $used_questions = array();
// note the []
$used_questions[] = $questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);

Upvotes: 4

Fernando Prieto
Fernando Prieto

Reputation: 520

The problem is that $used_questions is storing the value stored in $questions_for_test[$difficulty_level][$question_id] and not the array.

So do this $this->session->set_userdata('used_questions',$questions_for_test);

Upvotes: 0

Related Questions