Advertisement
Guest User

PHP Shopping Cart

a guest
Feb 21st, 2012
2,909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?
  2. session_start();
  3.  
  4. function showCart() {
  5.     $cart = $_SESSION['cart'];
  6.     if ($cart) {
  7.         $items = explode(',',$cart);
  8.         $contents = array();
  9.         foreach ($items as $item) {
  10.             $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
  11.         }
  12.         foreach ($contents as $id=>$qty) {
  13.             $output[] = 'You have '.$qty.' items with ID: '.$id;
  14.         }
  15.     }
  16.     else {
  17.         $output[] = 'You don\'t have items in your cart';
  18.     }
  19.     return join('',$output);
  20. }
  21.  
  22. if(isset($_SESSION['cart'])) {
  23.     $cart = $_SESSION['cart'];
  24. }
  25. else {
  26.     $cart = 0;
  27. }
  28.  
  29. if(isset($_GET['action'])) {
  30.     $action = $_GET['action'];
  31.     switch ($action) {
  32.         case 'add':
  33.             if ($cart) {
  34.                 $cart .= ','.$_GET['id'];
  35.             } else {
  36.                 $cart = $_GET['id'];
  37.             }
  38.             break;
  39.         case 'delete':
  40.             if ($cart) {
  41.                 $items = explode(',',$cart);
  42.                 $newcart = '';
  43.                 foreach ($items as $item) {
  44.                     if ($_GET['id'] != $item) {
  45.                         if ($newcart != '') {
  46.                             $newcart .= ','.$item;
  47.                         } else {
  48.                             $newcart = $item;
  49.                         }
  50.                     }
  51.                 }
  52.                 $cart = $newcart;
  53.             }
  54.             break;
  55.         case 'update':
  56.         if ($cart) {
  57.             $newcart = '';
  58.             foreach ($_POST as $key=>$value) {
  59.                 if (stristr($key,'qty')) {
  60.                     $id = str_replace('qty','',$key);
  61.                     $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
  62.                     $newcart = '';
  63.                     foreach ($items as $item) {
  64.                         if ($id != $item) {
  65.                             if ($newcart != '') {
  66.                                 $newcart .= ','.$item;
  67.                             } else {
  68.                                 $newcart = $item;
  69.                             }
  70.                         }
  71.                     }
  72.                     for ($i=1;$i<=$value;$i++) {
  73.                         if ($newcart != '') {
  74.                             $newcart .= ','.$id;
  75.                         } else {
  76.                             $newcart = $id;
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.         $cart = $newcart;
  83.         break;
  84.     }
  85. }
  86.  
  87. $_SESSION['cart'] = $cart;
  88.  
  89. echo showCart();
  90. echo '<br/>';
  91. echo '<a href="?action=add&id=1">Add to Cart</a>';
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement