Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. <?php
  2. if (isset($_POST ['pid'])) {
  3. $pid = $_POST ['pid'];
  4. $wasFound = false;
  5. $i = 0;
  6. if (!isset ($_SESSION["cart_array"]) || count($_SESSION ["cart_array"])<1){
  7. $_SESSION["cart_array"] = array (0=> array("item_id"=> $pid, "quantity" => 1));
  8. } else {
  9. foreach ($_SESSION["cart_array"] as $each_item) {
  10. $i++;
  11. while(list($key,$value)=each($each_item)){
  12. if ($key== "item_id" && $value == $pid) {
  13. array_splice ($_SESSION["cart_array"], $i-1,1, array(array("item_id"=>$pid,"quantity"=> $each_item['quantity']+1)));
  14. $wasFound = true;
  15. }
  16. }
  17. }
  18. if($wasFound==false) {
  19. array_push ($_SESSION["cart_array"], array("item_id"=> $pid, "quantity" => 1));
  20. }
  21. }
  22. header("Location: cart.php");
  23. exit();
  24. }
  25. ?>
  26.  
  27. if (isset($_POST['pid'])) {
  28. // add (+1) item to cart
  29. $pid = (int)$_POST['pid']; // cast as integer
  30. // valid pids are > 0
  31. if($pid > 0){
  32. if(!isset($_SESSION['cart_array'][$pid])){
  33. // item is not in the cart, add it with quantity = 1
  34. $_SESSION['cart_array'][$pid] = array("item_id" => $pid, "quantity" => 1); // I left the array in the cart the same, but it could also be simplified so that it is only the quantity, since the item_id is now the cart array index
  35. } else {
  36. // item is in the cart, increment quantity
  37. $_SESSION['cart_array'][$pid]['quantity']++;
  38. }
  39. }
  40. header("location: cart.php");
  41. exit();
  42. }
  43.  
  44. if ($result = print_r(array_keys('cart_array',$pid))) {
  45. $comma_seperated = implode("," $result);
  46. // then use use $comma_seperated in query where needed later in annother code block?
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement