session does not keep the values - php

I have an array of variables and I need to show its values.
So at first I send "me" variable to copy the array into session then I send "mypage" variable to show the first variable of the array but when I send "mypage" variable it seems the session is empty as show function does not show anything.
Please advice if you think I should use any other method as I am doing this to implement pagination.
<?php session_start();
if(isset($_GET["me"]) //if me variable is received copy the array in session
{
$myarray = array("foo", "bar", "hallo", "world");
$_SESSION["mysession"] = $myarray;
}
if(isset($_GET["mypage"]) // if mypage variable is received go to show function
show()
show() // show value 1 of the session
{
$values = array();
$values= $_SESSION['mysession'];
echo $values[1];
}
?>

Your code is very vague though for your question it seems that you are accessing SESSION with wrong key.
As you are setting session with mysession key above. Try to change below in your show function,
$values= $_SESSION['mysession'];

Related

Array_push pushes invisible element, and only works on second push

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.

Can't call array record in second page PHP

I've two page , first page there's an array data and second page I want call array data
Like this
First page index.php
$array_data[]=$array_tmp;
print_r($array_data); // array can display in this page
$_SESSION['one'] = $array_data;
Second page next.php
I want to call array from first page
session_start();
$array = $_SESSION['one'];
foreach( $array as $key => $value ) {
echo $value;
}
print_r($_SESSION['one'])
May I know what's wrong? As array can't display in second page.
I think you should change this code $array = $_SESSION['one']; to $array[] = $_SESSION['one'];. I`m not sure & not tested as well but I think so. Hope this will help.
You need to start the session if you already have not. Without starting the session, you can't assign value to the session variable. So the first code snippet would be like this:
session_start();
$array_data[]=$array_tmp;
print_r($array_data); // array can display in this page
$_SESSION['one'] = $array_data;
Second snippet looks fine but the last line is missing a semicolon. It might sound silly, but that can prevent the whole script from running. Here's the code fixed.
session_start();
$array = $_SESSION['one'];
foreach( $array as $key => $value ) {
echo $value;
}
print_r($_SESSION['one']);
Post more code if this does not work.

Storing variables in SESSION array

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

PHP $_SESSION data overwritten after each $_POST

I'm sending data over to a PHP file and storing it using $_SESSION. I'd like to post multiple instances of data to the session at different times and view these on the front end. Currently I can store one item in my session and this is displayed on the front end, however I'm struggling to add further instances of artist and title as it simply gets overwritten every time I $_POST data across.
How do I store and display an array of $_POST data in the session variable? I've tried adding a further set of [] after $_SESSION['artist'] and $_SESSION['title'] but this doesn't work.
<?php
session_start();
if(isset($_POST['artist']))
{
$_SESSION['artist'] = $_POST['artist'];
}
if(isset($_POST['title']))
{
$_SESSION['title'] = $_POST['title'];
}
print_r($_SESSION['artist']);
echo "<br>";
print_r($_SESSION['title']);
?>
You'll need to use an array to hold these values, so $array[] = $value will append $value to the $array.
Example:
if(isset($_POST['title'])) {
// Append POST data to SESSION
$_SESSION['titles'][] = $_POST['title'];
}
print_r($_SESSION['titles']);
You don't need to make sure $array is actually an array, since [] will make the variable an array if needed: Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.
<?php
session_start();
if(isset($_POST['artist']))
{
$_SESSION['artist'][] = $_POST['artist'];
}
if(isset($_POST['title']))
{
$_SESSION['title'][] = $_POST['title'];
}
print_r($_SESSION['artist']);
echo "<br>";
print_r($_SESSION['title']);
?>
user array to store it.

i can`t store session value passing through jquery ajax. in codeigniter php framework

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

Categories