Array Cart
Posted: Sun Jun 22, 2003 7:16 am
I'm trying to make this cart script, that is adding products and the quantity for the product. It then saves the array in a session, and when the customer at a later point adds a new item the script checks if session is allready set. If it is it continues with that array, just adding a new product.
The problem is that if there aren't any products stored in the array it doesn't work, and if there are 1 product in it, it want add more than one.
Script:
mod_edit: changed to
The problem is that if there aren't any products stored in the array it doesn't work, and if there are 1 product in it, it want add more than one.
Script:
Code: Select all
<?php
session_start();
$product = $_GET['id'];
if( empty($HTTP_SESSION_VARS['cart']) )
{
$cart="array()";
$current_items="0";
}
else
{
$cart = $HTTP_SESSION_VARS['cart'];
$current_items= $HTTP_SESSION_VARS['current_items'];
}
//Adding the new product
$cart[$current_items]['product'] = "$product";
$cart[$current_items]['quantity'] = "1";
$current_items++;
session_register("cart");
session_register("current_items");
//Looping thru the array
foreach( $cart as $temp )
{
foreach( $temp as $key=>$final_val )
{
print("$key: $final_val<br>");
}
print("<br>");
}
?>Code: Select all
tags[/size]