Help Me Understand This Shopping Cart Code Please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Help Me Understand This Shopping Cart Code Please

Post by nitediver »

I found these code, but it doesn't have any explanation.
Anyone can help me to understand how these code work.
Last edited by nitediver on Wed Jun 09, 2010 7:37 am, edited 1 time in total.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help Me Understand This Shopping Cart Code Please

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Help Me Understand This Shopping Cart Code Please

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help Me Understand This Shopping Cart Code Please

Post 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;

?>
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Help Me Understand This Shopping Cart Code Please

Post 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.
Last edited by nitediver on Wed Jun 09, 2010 7:39 am, edited 1 time in total.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Help Me Understand This Shopping Cart Code Please

Post 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.
Post Reply