Storing variables accross a shopping cart

Posted on 2004-11-19
Last Modified: 2013-11-29
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

Question by:pjcrooks2000
    LVL 9

    Expert Comment

    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:
    LVL 9

    Assisted Solution

    sorry, I was a little quick elaborating, now that i had time to think it through:
    LVL 8

    Author Comment

    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 :)
    LVL 27

    Accepted Solution

    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.
    LVL 9

    Expert Comment

    @diablo: you should have used the word tutorial in your post, then it would be included in my elaboration ;)
    great explanation
    LVL 8

    Author Comment

    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.


    LVL 27

    Expert Comment

    @ _GeG_  


    Thank you

    @ pjcrooks2000

    no problem, best of luck with your script

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now