• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

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

pjcrooks2000
0
pjcrooks2000
Asked:
pjcrooks2000
  • 3
  • 2
  • 2
2 Solutions
 
_GeG_Commented:
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 http://oscommerce.org/
oh, let me elaborate: http://www.google.at/search?q=tutorial+shopping+cart
0
 
_GeG_Commented:
sorry, I was a little quick elaborating, now that i had time to think it through: http://www.google.at/search?hl=de&q=tutorial+shopping+cart+php
0
 
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 :)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Diablo84Commented:
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.

1)

session_start();

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:

<?php
session_start();
$_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:

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

//OR

$_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.

<?php
$count = count($_SESSION['basket']);
echo "$count items in basket";
?>

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

<?php
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.
0
 
_GeG_Commented:
@diablo: you should have used the word tutorial in your post, then it would be included in my elaboration ;)
great explanation
0
 
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.

:)

pjcrooks2000
0
 
Diablo84Commented:
@ _GeG_  

:)

Thank you

@ pjcrooks2000

no problem, best of luck with your script
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now