1
session_start();
$imgList = $_REQUEST['imgList']; //comes through post (jquery)
$i = 0;
$a = explode(',', $imgList); 
$abc = count($a);

for ($e = 0; $e < $abc; $e++)
{
  echo $_SESSION['c+$i']=$a[$e];
  $i++;
}

echo $_SESSION['c+$i']; //this returns the last value
echo count($_SESSION['c+$i']); //returns only length 1
echo count($i); // returns only length 1

I don't know why more than one variable are not initilizing. can anyone please tell me about htis problem or fix this.i"ll be very thankful to you for this favor

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
aqib
  • 39
  • 8
  • What is the value of `$_REQUEST['imgList']`/`$a`? – JustBaron Jul 21 '17 at 08:04
  • You are using single quotes in your `$SESSION` array key, hence, it will always literally be `c+$i`, no matter what value `$i` has. I suggest you read [this thread](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php). – roberto06 Jul 21 '17 at 08:04
  • $("#check").click(function(){ var data = $('input:checkbox:checked').map(function(){ return this.value; }).get(); var dataString = "imgList="+ data; $.post('filter.php',dataString,function(theResponse){ alert(theResponse); – aqib Jul 21 '17 at 08:09
  • @roberto06 i did it but its not showing any result and givin an error – aqib Jul 21 '17 at 08:11
  • `$_SESSION[]` is an array. Read more about [PHP array](http://php.net/manual/en/language.types.array.php) and how to use them. Also read about [strings](http://php.net/language.types.string). The section about [single quoted strings](http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single) could help you understand half of your mistakes in the posted code. – axiac Jul 21 '17 at 08:19

4 Answers4

2

Change your code completely like below:-

session_start();
$_SESSION = array();
$imgList = $_REQUEST['imgList']; //comes through post (jquery)
$a = explode(',', $imgList); 
$_SESSION['c'][] = $a;
print_r($_SESSION['c']);

Now on other page if you need to access this SESSION data, do like below:-

session_start();
print_r($_SESSION['c']);
foreach($_SESSION['c'] as arr){
  echo $arr."\n";
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1
 echo $_SESSION['c+$i']=$a[$e];

please use "c+$i" instead of 'c+$i'.

Liu.handy
  • 59
  • 3
  • when i echo count($_SESSION["c+$i"]); this turn undefiend index – aqib Jul 21 '17 at 08:15
  • Of course it show underfiend,$i is a variable,after loop,the c+$i is more 1 than the last index. remove all echo count()...,and use var_dump($_SESSION),you can get all. – Liu.handy Jul 21 '17 at 08:20
1

As I said in my comment, you are using single quotes in your $SESSION array key, hence, it will always literally be c+$i, no matter what value $i has.

You need to use either double quotes :

$_SESSION["c+$i"]=$a[$e];

Or the PHP concatenating operator :

$_SESSION['c+' . $i] = $a[$e];

BTW, there's no need for an echo there, as there's no need for a $i variable, since $e already ranges from 0 to $abc and is equal to $i in each for iteration.

Also, count($i) will always return 1 , since $i is an int.

roberto06
  • 3,844
  • 1
  • 18
  • 29
  • i want to use these session variables on other page that is why usin $_SESSION["c+$i"]=$a[$e]; – aqib Jul 21 '17 at 08:18
  • but how can i use these variables on other page? – aqib Jul 21 '17 at 08:18
  • I wouldn't use `c+$i`, I would build an array inside `$_SESSION['c']`, by using `$_SESSION['c'][$i]`. This way, `$_SESSION['c']` will contain all your values, you'll just have to loop through it to retrieve the one you want. – roberto06 Jul 21 '17 at 08:20
1
session_start();

// More important evalutate vars to avoid log errors en your server
if( isset( $_REQUEST['imgList'] ) && !empty( $_REQUEST['imgList'] ) )
{
    // $imgList = $_REQUEST['imgList']; remove, make direct,
    // $i = 0; remove, optimize recourses, 
    $a = explode(',', trim( $_REQUEST['imgList'], "," ) );  // trim to remove empty positions in array
    // $abc = count($a); remove, optimize recourses, free memory

    for ($e = 0; $e < count( $a ); $e++)
    {
        $_SESSION[ "c_".$e ] = $a[ $e ]; // remove echo, "+" what is ?? separator?? if it is, change by underscope
    }   
    // echo $_SESSION["c_0"]; // 0,1,2 at length $a -1
    // echo count($_SESSION['c+$i']); // you count string ??? or array???, I don't understand this
    // echo count($i); // returns only length 1 // count a one number????, I don't understand this

    // to print
    for ($e = 0; $e < count( $a ); $e++)
    {
        echo $_SESSION[ "c_".$e ]; // print value of session in position $e
    }

    echo count( $e ); // Number of vars sesions created!

    // to remove
    for ($e = 0; $e < count( $a ); $e++)
    {
        unset( $_SESSION[ "c_".$e ] ); // remove var session in position $e
    }
}
MikeSouto
  • 265
  • 1
  • 7