Untitled

 avatar
unknown
plain_text
2 years ago
760 B
3
Indexable
session_start();
if(!isset($_SESSION['cart'])) {
  $_SESSION['cart'] = array();
}

if(isset($_POST['product_id']) && isset($_POST['product_name']) && isset($_POST['price']) && isset($_POST['quantity'])) {
  $item = array(
    'product_id' => $_POST['product_id'],
    'product_name' => $_POST['product_name'],
    'price' => $_POST['price'],
    'quantity' => $_POST['quantity']
  );
  array_push($_SESSION['cart'], $item);
}

// Display cart contents
$total = 0;
foreach($_SESSION['cart'] as $item) {
  $subtotal = $item['price'] * $item['quantity'];
  $total += $subtotal;
  echo '<p>' . $item['product_name'] . ' - $' . $item['price'] . ' x ' . $item['quantity'] . ' = $' . $subtotal . '</p>';
}
echo '<p>Total: $' . $total . '</p>';
Editor is loading...