Reputation: 1458
I am building a shopping cart and in my task I have to print out the items that the customer bought. I have tried to store the items in an array $_SESSION['items']
but no success. I have tried this:
$_SESSION['items'][] = $item;
but it did not work.
Please give some advice?
Upvotes: 0
Views: 14777
Reputation: 1973
In my case, I was augmenting some existing base $_SESSION
variable.
For example, I was initially just setting:
$_SESSION['amount'] = 4300
Then I tried to add a display 'sub-variable':
$_SESSION['amount']['Display'] = $4,300.00.
But I found that the second operation overwrote part of the base variable.
The fix was to do things explicitly:
$_SESSION['amount']['Amount'] = 4300;
$_SESSION['amount']['Display'] = $4,300.00
Upvotes: 0
Reputation: 1
$_SESSION['req_id_in_sess'] = array();
$_SESSION['req_id_in_sess'] = $req_id; //$req_id is array
foreach($_SESSION["req_id_in_sess"] as $key => $val)
{
echo $val,"<br/>";
}
//for single output
echo $_SESSION["req_id_in_sess"][0];
Upvotes: 0
Reputation: 187
for using session variables you have to start session using session_start();
to add elements try $_SESSION['items'][]=$items;
and to print session variable try print_r($_SESSION['items'][]);
or
foreach ($_SESSION['items'][] as $item)
{
echo $item;
}
Upvotes: 1
Reputation: 157414
Did you use session_start()
? You need to declare session_start()
before you use $_SESSION
in order to save values inside a session variable.
Also you are using a session array, so use print_r($_SESSION['items'])
to see what it outputs, inorder to access the array value you need to specify the index too, for example
echo $_SESSION['items'][0]
Upvotes: 1
Reputation: 2187
use session_start();
to declare session.
and use $_SESSION['items'][] = $item;
Should work..
Upvotes: 1