I have a variable which changes its value in every ajax request.
What I want to accomplish is to have a session array $_SESSION["tmp_arr"] and fill it with the values of this same variable. I dont want to overwrite the value of the $_SESSION variable, but append it in a array.
Which is the correct way to accomplish that?
If there is no such element in _SESSION or if it's not an array create a new one with the first/initial value. Otherwise append the new value to the existing array.
session_start();
[...]
if ( !isset($_SESSION["tmp_arr"]) || !is_array($_SESSION["tmp_arr"]) ) {
$_SESSION["tmp_arr"] = array( $newValue );
}
else {
$_SESSION["tmp_arr"][] = $newValue;
}
Related
I want to add an element to PHP array when form is submitted and then add that array to $_SESSION so I can display it on other page while $_SESSION is active, but when an element is added to an array, element that is already in it is deleted so I constantly have 1 item in array. Any suggestions?
Here's the code:
$korpa = array();
$_SESSION["korpa"] = $korpa;
if(isset($_POST["add"])){
array_push($korpa, $_POST["id"]);
}
You keep assigning an empty array to your session variable, so it will be empty at the start of your script, before you append the POST variable.
Instead, you can append directly to that session variable if the condition is met.
// Initialize the session array if its not set
if (!isset($_SESSION["korpa"])) {
$_SESSION["korpa"] = [];
}
// Then append the POST value to the session if that's set
if (isset($_POST["add"])) {
$_SESSION["korpa"][] = $_POST["add"];
}
Naturally you will need to call session_start() at the top of every page using sessions, otherwise they will not be set across your different pages.
I had a problem with my array_push, that i noticed.
So what i'm doing.:
I have a site, where there are some buttons with a specific value.
Each value is getting fetched from a database.
I have a session called test, that get's converted to an array(to store multiple in the same array)
Everytime one of the buttons are clicked, the value for that specific button, is getting pushed to the array.
But, i can ONLY see that it has been pushed at the second try.
[test] => Array( [0] => 21304 )
This is what i see, after second try. But my array count, says that there are 2 elements, in that array.
Here is my code:
if(isset($_POST['process'])) {
if(!isset($_SESSION['test'])) {
$_SESSION['test'] = array();
$array_merge = array_push($_SESSION['test'], $_POST['process']);
}
}
The $_POST['process'] is the button with the unique value.
Can somebody maybe see what I'm doing wrong here?
Kind regards
You are only adding to the $_SESSION['test'] array if $_SESSION['test'] was not previously set.
So you need to always add an occurance to the session array and only initialise the session array if it was not previously set
session_start();
// ...
if(isset($_POST['process'])) {
if(!isset($_SESSION['test'])) {
$_SESSION['test'] = array();
}
$_SESSION['test'][] = $_POST['process'];
}
NOTE from the manual
If you use array_push() to add one element to the array, it's better to use $array[] = because in that way there is no overhead of calling a function.
I am having a php session array like
('10/01/2017, '13/02/2017', '21/21/2107')
Now how to add and element or remove an element from this array in O(1)
The easiest way is to get the value, remove the item, and set the session variable again.
$data = $_SESSION['array']; // Get the value
unset($data[1]); // Remove an item (hardcoded the second here)
$_SESSION['array'] = $data; // Set the session value with the new array
Update:
Or like #Qirel said, you can unset the item directly if you know the number.
unset($_SESSION['array'][1]);
Update 2
If you want to remove the element by its value, you can use array_search to find the key of this element. Note that if there are to elements with this value, only the first will be removed.
$value_to_delete = '13/02/2017';
if (($key = array_search($value_to_delete, $_SESSION['array'])) !== false)
unset($_SESSION['array'][$key]);
To delete and element from an array use unset() function:
<?php
//session array O
unset(O["array"][1]);
?>
I want to set a value in a Codeigniter session array, something like:
$this->session->userdata['xxx']['yyy'] = $some_value;
But it didn't work. Also, I've tried:
$this->session->sess_write();
But that did not work either for me. Does anyone know how I can assign a value to this kind of session?
You can set multi dimensional array with this example.
$row => array(
'basket_id'=>'1',
'order_id'=>'1',
'cus_id'=>$cusid,
'product_id'=>$pro_id,
'bas_quantity'=>1
)
$cart = array($row);
$this->session->set_userdata('cart',$cart);
You can set multi-dimensional array as below.
$this->session->set_userdata(array("index_1"=>array("val1","val2","val3")));
Then you can use
$sess_data = $this->session->userdata("index_1");
echo $sess_data[0];
echo $sess_data[1];
echo $sess_data[2];
You cannot directly unset specific value of multidimensional array in session of codeingiter. You have to take all session value to variable. Unset the specific value and again set the variable in session as shown below:
$items_session = $this->session->userdata('item_names');
unset($items_session[$key][$app_no]);
$this->session->set_userdata('item_names',$items_session);
Hope this will help.
I have a problem storing session value;
I am not able to increment the session count variable
via the AJAX Jquery call with some value passing
to function, also my previously set array which stores the session is change with a new one.
What is the problem here:
1>when i use one controller function
startOnlineTest($testid=0)
i set session countnew as 0
$this->session->set_userdata('countnew',0);
and in view i use jquery to pass data to other function of same controller
public function ResponseOnline($qid,$response)
using change effect of jquery
echo "[removed]$(document).ready(function(){";
foreach($question['childQuestion'] as $q=>$aq){ //
echo "\$(\"input:radio[name=qid-$q]\").live('change',function(){
var sr=\$(\"input:radio[name=qid-$q]:checked\").map(function() {
return $(this).val();
}).get().join();
var qid = \$(\"input:radio[name=qid-$q]\").attr(\"qaid\");
"; echo "\$.get('"; echo base_url();echo "testpapers/ResponseOnline/'+qid+'/'+sr,{},function(data)
{\$('#res').html(data)});
});";}
echo"});[removed]" ;// this script is working fine
now the problem is this i get the session value overwrite by the current one although i
use array my code for ResponseOnline
is
public function ResponseOnline($qid,$response)
{
echo "this" .$this->session->userdata('countnew'); // this is not echo as set above by function startOnlineTest($testid=0)[/color]
i set session countnew as
$this->session->set_userdata('countnew',0)
echo $d=$qid; // i know its no use but to save time as tested on $d
$s=$response;
if($this->session->userdata('countnew')==0) // algo for this function i check the
//countnew session varaible if 0 do this
{
$sc=$this->session->userdata('countnew'); // store the countnew variable
echo $sc=$sc+1; // increment the counter variable
$this->session->set_userdata('countnew',$sc); // store it in session
echo "this is incrementes session value";
echo $this->session->userdata('countnew');
$r2[$d]=$s; // store array value $r2 with key $d and value $s
$this->session->set_userdata('res',$r2); //set this array value in session
}
else{ // if session countnew!=0 then do this
$r2=$this->session->userdata('res'); // first store $r2 as array return from session
$r2[$d]=$s; //then add value to this array
$this->session->set_userdata('res',$r2); // storing this array to session
}
echo '<pre>';
print_r($r2); // printing the array
}
i get the result for say for first call is fine but for second call my value is overwrite session show Array([32323]=>23)) if i pass function (32323,23) if i pass (33,42)
i get Array([33]=>42) my old value is destroyed.
You should just probably use regular sessions in php because sessions in codeigniter will only allow you to store a single item.
Something like this:
$_SESSION['item'][] = $array;
Or maybe you could still use CodeIgniter sessions by doing something like this:
$items = $this->session->userdata('items');
array_push($items, $new_item);
$this->session->set_userdata('items', $items);
If you want to define the keys as well:
$items = $this->session->userdata('items');
$items['your_key'] = $new_item;
$this->session->set_userdata('items', $items);