2

Hey guys this is the code I am using

$ch="s1";
$_SESSION[$ch] = array();
$_SESSION[$ch][] = $_POST['t0'];
$_SESSION[$ch][] = $_POST['t1'];
$_SESSION[$ch][] = $_POST['t2'];
$_SESSION[$ch][] = $_POST['t3'];
$_SESSION[$ch][] = $_POST['t4'];
$_SESSION[$ch][] = $_POST['t5'];
$_SESSION[$ch][] = $_POST['t6'];

But when I am printing the session data on screen $_SESSION['s1'][0] is having all the data of all the other index, $_SESSION['s1'][1] to $_SESSION['s1'][6] are not displaying anything.

Here is the result for var_dump($_SESSION);

's1' => array (size=7) 
   0 => string 'All Purpose Horn Anvil A-1t1=Shipping Weight: 900gmst2=Price: € 5.00t3=Quantity: t4= t5=56t6=All Purpose Horn Anvil A-1 Shipping Weight: 900gms Price: € 5.00 Quantity: 56' (length=192) 
    1 => null 
    2 => null 
    3 => null 
    4 => null 
    5 => null 
    6 => null

For some reason the POST indexes i.e t0 to t6 are been shown in s1.

4
  • Post the result of var_dump($_SESSION); Commented Oct 8, 2013 at 14:00
  • Done, I don't know why all the Post data are getting appended to the first index. Commented Oct 8, 2013 at 14:09
  • it would be nice if you could mark an answer as accepted. :) Commented Oct 9, 2013 at 19:22
  • Actually none of the replies are answering my question, it was a mistake in jquery which I posted below as the answer. :) Commented Oct 10, 2013 at 11:16

4 Answers 4

1

Propably you overwrite that data in other place or just do a mistake in printing.

$ch="s1";
$_SESSION[$ch] = array();
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
print_r( $_SESSION );

Output:

Array ( [s1] => Array ( [0] => 158 [1] => 145 [2] => 110 [3] => 139 [4] => 153 [5] => 183 [6] => 51 ) )

Sign up to request clarification or add additional context in comments.

2 Comments

I don't understand, please give details.
The problem is not in code fragment that you have posted - the problem is in other place.
1

It was a mistake in passing data to POST. In Jquery I was using this without the "&" between the variables, that was causing the problem.

var dataString = 't0=' + txt[0]+ '&t1='+ txt[1] + '&t2='+ txt[2] + '&t3='+ txt[3] + '&t4='+ txt[4] + '&t5='+ txt[5] + '&t6='+ temp;

Comments

0

Try to add a dot before equal:

    $ch="s1";
    $_SESSION[$ch] = array();

    $_SESSION[$ch][] .= $_POST['t0'];
    $_SESSION[$ch][] .= $_POST['t1'];
    $_SESSION[$ch][] .= $_POST['t2'];
    $_SESSION[$ch][] .= $_POST['t3'];
    $_SESSION[$ch][] .= $_POST['t4'];
    $_SESSION[$ch][] .= $_POST['t5'];
    $_SESSION[$ch][] .= $_POST['t6'];

You can see this code working on this link (3v4l.org/8ZDi9):

<?php

    $ch="s1";
    $_SESSION[$ch] = array();

    $_SESSION[$ch][] .= 11;
    $_SESSION[$ch][] .= 22;

    var_dump($_SESSION);

4 Comments

This seems to be wrong, empty [] means array_push (bit optimized one). There is no item to concatenate with in this moment.
It will work if you have suppressed error reporting, because you concatenate 11 to undefined variable. PHP will allow you to do so, but however it cannot help the guy question, it will act the same way as without the ..
This also turns the integers into strings.
I would bet on $str = (string) $integer.
0
$ch="s1";
$_SESSION[$ch] = array();
$_SESSION[$ch][0] = $_POST['t0'];
$_SESSION[$ch][1] = $_POST['t1'];
$_SESSION[$ch][2] = $_POST['t2'];
$_SESSION[$ch][3] = $_POST['t3'];
$_SESSION[$ch][4] = $_POST['t4'];
$_SESSION[$ch][6] = $_POST['t5'];
$_SESSION[$ch][7] = $_POST['t6'];

Or better

$ch="s1";
$_SESSION[$ch] = array();


for ($i=0; $i<=6; $i++)
{
   $_SESSION[$ch][$i] = $_POST['t'.$i];
}

1 Comment

Fix this row: $_SESSION[$ch][6] = $_POST['t5'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.