Forum Moderators: coopster
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 />';
}
}
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
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']);
}
Arrays in sessions work just like everywhere else. A good read regarding the subject is right on the PHP manual page: arrays [php.net]