Page 1 of 1
Help Me Understand This Shopping Cart Code Please
Posted: Mon May 10, 2010 9:56 am
by nitediver
I found these code, but it doesn't have any explanation.
Anyone can help me to understand how these code work.
Re: Help Me Understand This Shopping Cart Code Please
Posted: Mon May 10, 2010 10:12 am
by AbraCadaver
nitediver wrote:I found these code, but it doesn't have any explanation.
Anyone can help me to understand how these code work.
Why this session appear in if condition without ever declared before?
Code: Select all
if (isset($_SESSION['cart'])) {
$cart = $_SESSION['cart'];
$_SESSION['cart'] is set at the end of the page, so the if is checking if the cart already exists in the session.
What is "&" mean on "&$item"?
Code: Select all
foreach ($cart as &$item) {
if ($item['product']['id'] == $id) {
$item['quantity'] += 1;
$in_cart = True;
break;
& is a reference to that variable. foreach makes a copy of the array so you can't change the array item from within the foreach unless you reference the item.
Why "$cart[] = array('product' => $product, 'quantity' => 1);" appear twice?
Code: Select all
if (!$in_cart) {
$cart[] = array('product' => $product, 'quantity' => 1);
}
} else {
$cart[] = array('product' => $product, 'quantity' => 1);
}
$_SESSION['cart'] = $cart;
The else the you show is from the original if at the top
if (isset($_SESSION['cart'])). If the session variable cart is set then loop through to see if the item is in the cart. If so then increment the quantity and if it is not in the cart then add it. The else says to add it to the cart if the session cart is not set.
Re: Help Me Understand This Shopping Cart Code Please
Posted: Mon May 10, 2010 10:25 am
by nitediver
I don't understand the whole concept of that code, please be more specific.
My conclusion is like this:
1.It GET the "id"
2.Select from database
3.It grab the product and put inside "cart session"
4.Put the session inside array
Is that correct?
Re: Help Me Understand This Shopping Cart Code Please
Posted: Mon May 10, 2010 10:59 am
by AbraCadaver
Code: Select all
<?php
session_start();
// the user clicked an add to cart link that had the product id in the URL: cart_add.php?id=10 or similar, so get that as $id
$id = $_GET['id'];
// get all of the info for that product id from the db
$result = mysql_query("SELECT * FROM products WHERE id=$id");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// fetch all of the product info into an array
$product = mysql_fetch_assoc($result);
// if the user already has a cart (they have added something to the cart and this script was called previously)
if (isset($_SESSION['cart'])) {
// make a copy of the cart for use
$cart = $_SESSION['cart'];
// assume that the product is not in the cart
$in_cart = False;
// loop through the existing cart products
foreach ($cart as &$item) {
// check to see if the requested product is already in the existing cart
if ($item['product']['id'] == $id) {
// if so add 1 to the quantity of the product in the cart
$item['quantity'] += 1;
// record the fact that the item is already in the cart so that it isn't added again
$in_cart = True;
break;
}
}
// if the requested product was not already in the cart, then add it as an array item
if (!$in_cart) {
$cart[] = array('product' => $product, 'quantity' => 1);
}
// if the user does not have a cart, then create a cart array with this product as the first array item
// if (isset($_SESSION['cart'])) is false
} else {
$cart[] = array('product' => $product, 'quantity' => 1);
}
// put the cart in the session
$_SESSION['cart'] = $cart;
?>
Re: Help Me Understand This Shopping Cart Code Please
Posted: Tue May 11, 2010 9:01 am
by nitediver
Thank you for explaning.
Now I try to write my own code similar to that one.
But something wrong with, the 2.php only display the last value from array.
Re: Help Me Understand This Shopping Cart Code Please
Posted: Thu May 13, 2010 10:23 am
by nitediver
I still don't understand how this code work, especially the part when the array put inside the session.
Can you give me a list of keyword/tutorial I should search & read, to help me understand this code.