Storing variables accross a shopping cart

Hi all,

I am making a website that is able to creating online bookings for four seperate products.

Can someone please explain to me the processes that normally used in industry for shopping cart type website.  e.g. is the data for each product stored into tables as each item is added and then upon the final submit of the purchase via a form does this then get copied into the main booking tables?

Or as products are selected are the purchases stored into PHP variables and passed from script to script?

This is a 500 point question so please justify your answers as only the best answers will be looked on with favour.  Also if you can ellaborate at all on the shopping cart type process with PHP and MySQL that would be very nice of you.

Many thanks to you all

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

they use $_SESSION for the cart for short time storage, put the order data in a database, if they want to store it for a longer period, and when the order is accepted/completed..., they put it in their books (main booking table if it is a very advanced system)
If you want to have a good example, look at
oh, let me elaborate:
sorry, I was a little quick elaborating, now that i had time to think it through:
pjcrooks2000Author Commented:
Hi _GeG_

Thanks for replying, it's quite late here at the moment and I have had a few shall we say.  I will give your answer due attention in the morning and respond to you asap :)
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

I think a little further elaboration would be helpful.

There are two sides to the coin here as _GeG_ has pointed out, for more permanent storage we have a database table and for temporary access of data we have sessions.

A session is valid from the moment the user starts browsing until they close their browser window. During this time data can be assigned to a session variable whereas it can be accessed on any page on your site during browsing session.

Using sessions is very simple, there are 3 parts to the process; initializing the session, assigning data to a session variable and using that data on another page.



This MUST go at the top of your script before any output* and serves the purpose of initializing the session data. This is to say if the session doesn't exist (ie. the first time they access the page) start the session, otherwise continue it.

*output can be:

php echo/print
html tags
new lines before <?php
a space <?php <-- here

It must go before any of the above.

IMPORTANT: session_start(); must appear on EVERY page that you wish to use the session data otherwise the data will not be initialized.

2) assigning a value to a session variable

$_SESSION['variable_name'] = "value";

Whe using a cart you will probably be assigning shopping items to the session variable based upon the value of a post form item or a get query string item, so it may be:

$_SESSION['item'] = $_GET['product'];

3) outputting the session variables value

echo $_SESSION['variable_name'];
//or assigning it to a local variable:
$var = $_SESSION['variable_name'];

Thats a fairly basic overview, with a concise example we have:

$_SESSION['var'] = "some value";
echo $_SESSION['var'];

Now something that will be useful to you is a session array.

Think of this as you would a normal array, only it is globaly available.

A session array will allow you to store all of the shopping cart items in one variable, you would add items to it like this:

$_SESSION['basket'][] = "new item to add";


$_SESSION['basket'][] = $_GET['product_id']; //for example

With this in mind here are a few useful things you can do with this session array.

Getting the number of items in the basket.

$count = count($_SESSION['basket']);
echo "$count items in basket";

Looping through each item (perhaps to display the basket contents):

foreach($_SESSION['basket'] as $item) {
 echo " - $item is in the basket<br>\n";

Now this part comes down to theory a little, you have to think of how you are going to work this system and take advantage of the session array.

Presumably you have all of your available items stored in a database along with their various pieces of information, basically a record per item. Hopefully each item will have a unique ID. If so the easiest approach is when you add items to the basket (or the session array) you store its id in the array and nothing else.

When it comes to processing the users basket, or displaying the information, when you loop through the items you could then use the ID to cross reference the information for that item in the table (eg. SELECT * FROM table WHERE item_id = 'id stored in session array'). You can then use the results from that query to display the item name, description, price etc etc.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
@diablo: you should have used the word tutorial in your post, then it would be included in my elaboration ;)
great explanation
pjcrooks2000Author Commented:
Yes I agree a great explanation diablo and thanks to you too GeG.

Here is justification on how I am accepting the answer:

The accepted answer shall be awarded to Diablo as I find the explanation to be very thorough and was exactly the sort of answer I was looking for.  However I cannot deny the assistance provided by GeG and I shall award the assisted answer and I will now close the question and award points as I see to be appropriate.

I thank you both for your input and hope to be able to call on your assistance in the future.


@ _GeG_  


Thank you

@ pjcrooks2000

no problem, best of luck with your script
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.