Welcome Guest, Not a member yet? Register   Sign In
Updating a session array
#1

(This post was last modified: 10-19-2018, 01:26 PM by Qodi.)

I would like to update a session array using the codeigniter session library terminology.

I have a session array named chairs. Each key of the array is the chairID of the associated chair and the value of the chairID key is the quantity of chairs of with that ID.


I am looking at how I can update the array to add chairs to the array. 


I know the php syntax (bold). Below it is a list of possible codeigniter session library approaches I've postulated. They are probably all incorrect, I would appreciate some guidance.


$_SESSION['chairs'][$chairID] = $chairQuant;
               
$this->sesison->chairs[$chairID] = $chairQuant; (Simply replacing $_SESSION['chairs'] with $this->sesison->chairs)
               
$this->session->chairs += [$chairID => $chairQuant] (using unary array union operator)
                
{


$array = $this->session->chairs; 
               
array_push($array, array($chairID => $chairQuant))
               
$this->session->set_userdata(chairs, $array);


}  (Seemingly the most terminologically faithful approach, assign the session array value to another array, update this array using the array_push, then set the session array with the value of this new array. This approach feels very long winded, considering the alternative(s).)

Would appreciate help!

Thanks

God bless.
Reply
#2

(This post was last modified: 02-01-2025, 06:22 AM by Sunchock.)

Hello Igniters,

Sorry to bring up such an old topic, I'm having the exact same problem.

I was wondering how to handle multi-level session variables using CodeIgniter?

For now, I'm using:
PHP Code:
// Initialization
session()->push('app1', ['subA' => []]);
// Creating a variable into app1/subA 
session('app1')['subA']['var01'] = "Such text"

But if I want to check elsewhere in the code if the var01 exists recursively, how do I achieve this?

PHP Code:
// For now, I use:
if (isset(session('app1') && isset(session('app1')['subA']) && isset(session('app1')['subA']['var01'])) 

// I would expect something like :
session('app1')->has('subA.var01');
// or
session()->has('app1.subA.var01'); 

Same problem if I want to delete subA but not others variables into app1 ?

PHP Code:
// For now, I use:
unset($_SESSION['app1']['subA']);

// I would expect something like :
session('app1')->remove('subA');
// or
session()->remove('app1.subA'); 


So I asked myself: am I doing things wrong?
I mean: what are the best practices for handling session subvariables?
Reply
#3

PHP Code:
$session->push('hobbies', ['sport' => 'tennis']); 

PHP Code:
<?php

$newdata 
= [
    
'username'  => 'johndoe',
    
'email'     => '[email protected]',
    
'logged_in' => true,
];

$session->set($newdata); 

https://codeigniter.com/user_guide/libra...sions.html
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#4

Pushing New Value to Session Data.

The push() method is used to push a new value onto a session value that is an array.
For instance, if the hobbies key contains an array of hobbies, you can add a new value
onto the array like so:

PHP Code:
<?php

$session
->push('hobbies', ['sport' => 'tennis']); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Sorry if my post wasn't explicit enough.

I have no problem creating multi-level session variables.

Unless I'm mistaken, I have the impression that there is no way to modify and/or delete variables at level 2 or higher using CodeIgniter. So I fell back on PHP's native methods (unset, isset, etc.).

I would just like to know what are the best practices for MODIFYing or DELETING variables at a level 2 or 3, etc. without impacting the other variables.

I have already read and reread the doc several times before posting here
Reply
#6

No way. Inside session(), it's just checking isset($_SESSION[$key]), so you check the primary key in has() get()...
You can save the array to a variable and already work with it, and overwrite the session at the end.
It's like working with SQL - get row data , processing, saving.
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB