its easier with the associated array but use the products unique productID as the reference
ie $_SESSION['cart'][$product
where $product_details is an array of the vaues you have in the question, to save my typing it all.
then you can :-
if(isset($_SESSION['cart']
{
// either do nothing, report an error OR add a futher lot again(method I prefer).
$_SESSION['cart'][$product
}
else
{
$_SESSION['cart'][$product
}
the same can then be done for deleting and amending products easily.
p.s. you can convert the whole thing to a string (implode) for storing in a database later (order history).
This way your records remain accurate and will not alter when you change a price.
Main Topics
Browse All Topics





by: ReapzPosted on 2004-03-21 at 08:40:01ID: 10644333
I've just created my first shopping cart for my girlfriends website. I'm not saying it's the best way to do it but here goes....
;quantity2 etc..."
Firstly, I didn't use an associative array for the basket, I used a string.
eg: "prodid1;quantity1:prodid2
You don't really need to store the price and product name in the session variable because you can pull them out of a database using the prodid as a reference when the user views their basket. Keeps it nice and simple that way.
So when the customer clicks 'Add to basket' I call my addtobasket.php which splits the string by : into an array which gives me my items as prodid;quantity. Then using a For... Next I iterate through the array and split up the prodid and the quantity. If it comes across a matching prodid it adds 1 to the quantity and then rebuilds the string. If it doesn't it adds :newprodid;newquantity to the end of the string.
To delete an item form the basket is very similar... split the string, iterate through the array and build a new string form the array items that you want to keep.
If you need to see my messy code just say and I'll post it.
Now I'm going to await the flaming of my methods! :)