Hi Guys.
I am learning how to build a simple shopping cart from a book.
The code is below:
session_start();
class Product {
private $productId;
private $productName;
private $price;
public function __construct( $productId, $productName, $price ) {
$this->productId = $productId;
$this->productName = $productName;
$this->price = $price;
}
public function getId() {
return $this->productId;
}
public function getName() {
return $this->productName;
}
public function getPrice() {
return $this->price;
}
}
$products = array(
1 => new Product( 1, "SuperWidget", 19.99 ),
2 => new Product( 2, "MegaWidget", 29.99 ),
3 => new Product( 3, "WonderWidget", 39.99 )
);
if ( !isset( $_SESSION["cart"] ) ) $_SESSION["cart"] = array();
if ( isset( $_GET["action"] ) and $_GET["action"] == "addItem" ) {
addItem();
} elseif ( isset( $_GET["action"] ) and $_GET["action"] == "removeItem" ) {
removeItem();
} else {
displayCart();
}
function addItem() {
global $products;
if ( isset( $_GET["productId"] ) and $_GET["productId"] >= 1 and $_GET["productId"] <= 3 ) {
$productId = (int) $_GET["productId"];
if ( !isset( $_SESSION["cart"][$productId] ) ) {
$_SESSION["cart"][$productId] = $products[$productId];
}
}
session_write_close();
header( "Location: shopping_cart.php" );
}
function removeItem() {
global $products;
if ( isset( $_GET["productId"] ) and $_GET["productId"] >= 1 and $_GET["productId"] <= 3 ) {
$productId = (int) $_GET["productId"];
if ( isset( $_SESSION["cart"][$productId] ) ) {
unset( $_SESSION["cart"][$productId] );
}
}
session_write_close();
header( "Location: shopping_cart.php" );
}
function displayCart() {
global $products;
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
<title> A shopping cart using sessions </title>
<link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<h1> Your shopping cart </h1>
<dl>
<?php
$totalPrice = 0;
foreach ( $_SESSION["cart"] as $product ) {
$totalPrice += $product->getPrice();
?>
<dt> <?php echo $product->getName() ?> </dt>
<dd> $ <?php echo number_format( $product->getPrice(), 2 ) ?>
<a href="shopping_cart.php?action=removeItem&productId=<?php echo
$product->getId() ?> "> Remove </a> </dd>
<?php } ?>
<dt> Cart Total: </dt>
<dd> <strong> $ <?php echo number_format( $totalPrice, 2 ) ?> </strong> </dd>
</dl>
<h1> Product list </h1>
<dl>
<?php foreach ( $products as $product ) { ?>
<dt> <?php echo $product->getName() ?> </dt>
<dd> $ <?php echo number_format( $product->getPrice(), 2 ) ?>
<a href="shopping_cart.php?action=addItem&productId=<?php echo
$product->getId() ?> "> Add Item </a> </dd>
<?php } ?>
</dl>
<?php
}
I understand most of this.
But the only two things I do not understand are:
1) The below code (from the above script):
<dl>
<?php
$totalPrice = 0;
foreach ( $_SESSION["cart"] as $product ) {
$totalPrice += $product->getPrice();
?>
<dt> <?php echo $product->getName() ?> </dt>
<dd> $ <?php echo number_format( $product->getPrice(), 2 ) ?>
<a href="shopping_cart.php?action=removeItem&productId=<?php echo
$product->getId() ?> "> Remove </a> </dd>
<?php } ?>
<dt> Cart Total: </dt>
<dd> <strong> $ <?php echo number_format( $totalPrice, 2 ) ?> </strong> </dd>
</dl>
I am mainly having difficulty figuring out how the above foreach works.
The above piece of code is supposed to loop through each item in the cart, displaying a name, price and remove link (to remove the product from the cart). The thing that doesn't make any sense to me is that when you click one of the add product links, that product is then displayed in the browser as soon as you add it. So I would normally assume that whenever another product it added, the foreach (above) is then going to loop through the $_SESSION["cart"], echoing the already added product (again) whenever another product is added.
The thing I do not understand is how come it doesn't do this? Don't get me wrong. I am glad it doesn't do this. But to me it looks like it should loop though (and echo) the already added (and echoed) products, each time a new product is added.
I do not understand why it doesn't do that?
2) The second thing I am having trouble understanding is - why can this script only add one of each product to the cart? And what additional script could I add so it can add more than one of the same product?