Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. <?php
  2. session_start();
  3. require_once 'db_connecter.php';
  4. $item_id = $_POST['productId'];
  5. $amount = $_POST['inpBuyAmount'];
  6.  
  7. $sql = "SELECT * FROM items WHERE itemcode='$item_id'";
  8.  
  9. $result = mysqli_query($link, $sql);
  10. $row = mysqli_fetch_assoc($result);
  11.  
  12. $item_name = $row['itemname'];
  13. $item_price = $row['out_price'];
  14. $total_price = $item_price * $amount;
  15.  
  16. // $_SESSION['cart'] = array( array($item_id), array($amount));
  17. //
  18. // header("Location: ../productpage.php?product=" . $row['itemname']);
  19.  
  20. // itemnavn, itemid, amount, price per item, total pris
  21.  
  22. $location=-1;
  23.  
  24. $item = array($item_id, $item_name, $amount, $item_price, $total_price);
  25. if(!isset($_SESSION['cart'])){
  26. $_SESSION['cart'] = array();
  27. }
  28. else{
  29. for ($i=0; $i < sizeof($_SESSION['cart']); $i++) {
  30. if($_SESSION['cart'][$i][0] == $item[0]){
  31. $location=$i;
  32. }
  33. }
  34. }
  35.  
  36. if($location == -1){
  37. array_push($_SESSION['cart'], $item);
  38. }else{
  39. $_SESSION['cart'][$location][2]+=$item[2];
  40. }
  41. for($a=0; $a < sizeof($_SESSION['cart']); $a++) {
  42. echo "Item id: " . $_SESSION['cart'][$a][0] . "</br>";
  43. echo "Item name: " . $_SESSION['cart'][$a][1] . "</br>";
  44. echo "Amount: " . $_SESSION['cart'][$a][2] . "</br>";
  45. echo "Item price: " . $_SESSION['cart'][$a][3] . "</br>";
  46. echo "Total price: " . $_SESSION['cart'][$a][4] . "</br>";
  47. //break;
  48. }
  49.  
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement