0

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?

3
  • That's all you need. How is this "not working"? The array never gets created? Are you using session_start() properly everywhere you're modifying $_SESSION? Commented Jun 1, 2013 at 6:39
  • 1
    You probably only have to do session_start() at the beginning of your page and life will be good Commented Jun 1, 2013 at 6:43
  • 1
    Make sure your session_start appears before any HTML, otherwise it won't work. Ensure your warnings are enabled so you can see any problems with session initialisation. Commented Jun 1, 2013 at 6:52

5 Answers 5

1

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]
Sign up to request clarification or add additional context in comments.

9 Comments

@DilshatAbduwalli use this and see what you get print_r($_SESSION['items'])
why does every one incorrectly say "top of the page" for session_start?
before usage, is not the top of the page, line 50 million is fine if you done use sessions until 50 million and 1
when i use print_r($_SESSION['items']) is is printing Arry [0] => [1] => [1] ..)
@DilshatAbduwalli Ya so if you want to echo out the particular value, use that particular index number/name, read my edited answer
|
1

use session_start(); to declare session. and use $_SESSION['items'][] = $item;

Should work..

4 Comments

actully @MarcB that only applies to cookie-based sessions
@MarcB Ya please make him understand, that what harms if we put session_start() at the top of the page
@dagon: it can harm trans-sid sessions as well, if you turn on the session AFTER you've already output some/all forms and/or links on the page. PHP will not reach back in time to rewrite perviously performed output with the trans_sid
well that applies to any variable you can't use it before it exists, nothing 'special' there.
1

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

Comments

0
$_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];

Comments

0

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

Comments

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.