Forum Moderators: coopster

Message Too Old, No Replies

Shopping Cart Array with sessions

         

flashover

3:39 pm on Mar 16, 2005 (gmt 0)

10+ Year Member



I swear everytime I get to arrays I end up getting mixed up and can't get them to do what I want. I am working on building a shopping cart and don't want to store items in the database until they are ready to submit the order.

Basically here is what I want:
An array called $_SESSION['cart]
I am pretty sure I need a multidimensional array like
$_SESSION['cart'][1] $_SESSION['cart][2]<-- Each time an item is added this is incremented.
Within $_SESSION['cart'][1] are all the values entered when that was submitted.

Then I have to figure out the output which I assume is
foreach ($_SESSION['cart'][$i] as $key => $value) {
This would go into a while loop and $i is incremented each time to display all the items in the cart.

Main Page
if (isset($_SESSION['ISBN'])) $_SESSION['new']++; else $_SESSION['new']=1;
add_to_cart();

Function Page
function add_to_cart();
{
@ $new = $_SESSION['new'];

if($new)
{
//new item selected create the empty cart
if(!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
$_SESSION['ID'] ='';
$_SESSION['QUANTITY'] = 0;
$_SESSION['TOTAL_ITEMS'] = 0;
$_SESSION['CALC_PRICE'] ='0.00';
}
if(isset($_SESSION['cart'][$new]))
{
$_SESSION['cart'][$new]++;
$_SESSION['ISBN']++;
} else {
$_SESSION['ISBN']=1;
$_SESSION['cart'][$new] = 1;
$_SESSION['ID'] = $_SESSION['ITEM'];
$_SESSION['QUANTITY'] = $_SESSION['QTY'];
$_SESSION['TOTAL_ITEMS'] = $_SESSION['items'];
$_SESSION['CALC_PRICE']=$_SESSION['total_price'];
}
}
if(isset($_GET['QTY']))
$isbn = $_SESSION['ISBN'];
{
foreach ($_SESSION['cart'] as $isbn => $qty)
{
if($isbn =='0')
unset($_SESSION['cart'][$isbn]);
else
$_SESSION['cart'][$isbn] = $isbn;
}
$_SESSION['ID'] = $_SESSION['ITEM'];
$_SESSION['QUANTITY'] = $_SESSION['QTY'];
$_SESSION['TOTAL_ITEMS'] = $_SESSION['items'];
$_SESSION['CALC_PRICE']=$_SESSION['total_price'];
}

if($_SESSION['cart']&&array_count_values($_SESSION['cart']))
show_cart($_SESSION['cart']);
else
{
echo '<p>There are no items in your cart</p>';
echo '<hr />';
}
}

ikke

6:39 pm on Mar 16, 2005 (gmt 0)

10+ Year Member



Funny,
I had this problem to. Look atThis [webmasterworld.com] post.
For more information if you don`t get out of it tell me.


I put it all to a one dimensional array got that from Wildeyefrank it works for me now. And I had the same idea to wait to put in the databse until the end

flashover

4:09 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



WOW! It took me some time to figure this one out. I had looked at the link you sent me but I really wanted to figure out how to do it with sessions and building on to the array.

This is might help others as well because I could not find a lot of information on building arrays using sessions.

if(!isset($_SESSION['cart']))
{
$CartItem = array ($_SESSION['ITEM'], $_SESSION['QTY'], $_SESSION['items'], $_SESSION['total_price']);
$_SESSION['cart'] = $CartItem;
} else {
array_push($_SESSION['cart'], $_SESSION['ITEM'], $_SESSION['QTY'], $_SESSION['items'], $_SESSION['total_price']);
}

coopster

11:32 pm on Mar 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, flashover.

Arrays in sessions work just like everywhere else. A good read regarding the subject is right on the PHP manual page: arrays [php.net]