Advertisement
Guest User

Untitled

a guest
Sep 6th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. <?php
  2. session_start();
  3. $product_ids = array();
  4. session_destroy();
  5.  
  6. // Check if the Add to Card button has been submited
  7.  
  8. if ($_POST['add_to_cart']) {
  9. echo"prvi if";
  10. if (isset($_SESSION['shopping_cart'])) {
  11. echo "nekaj je bilo dodano v košarico";
  12.  
  13. //keep track of how many products are in the cart
  14. $count = count($_SESSION['shopping_cart']);
  15.  
  16. $product_ids = array_column($_SESSION['shopping cart'], 'id');
  17. pre_r($product_ids);
  18. } else { // if the shopping cart doesnt exist, create first product with array key 0
  19. //create array using submitted form data, start from key 0 and fill it with values
  20. echo"else";
  21. $_SESSION['shopping_cart'][0] = array(
  22. "id" => $_GET['id'],
  23. 'name' => $_POST['name'],
  24. 'price' => $_POST['price'],
  25. 'quantity' => $_POST['quantity'],
  26.  
  27. );
  28. }
  29. }
  30. pre_r($_SESSION);
  31. print_r($_SESSION);
  32. function pre_r($array)
  33. {
  34. echo '<pre>';
  35. print_r($array);
  36. echo '</pre>';
  37. }
  38.  
  39. ?>
  40. <!DOCTYPE html>
  41. <html>
  42.  
  43. <head>
  44.  
  45. <title>Online Shopping</title>
  46.  
  47. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
  48. crossorigin="anonymous">
  49. <link rel="stylesheet" href="cart.css">
  50. </head>
  51.  
  52.  
  53.  
  54. <body>
  55.  
  56.  
  57. <div class="container">
  58. <?php
  59.  
  60. $servername = "localhost";
  61. $username = "root";
  62. $password = "";
  63. $dbname = "cart";
  64.  
  65. // Create connection
  66. $conn = new mysqli($servername, $username, $password, $dbname);
  67. // Check connection
  68. if ($conn->connect_error) {
  69. die("Connection failed: " . $conn->connect_error);
  70. }
  71.  
  72. $sql = "SELECT * FROM products ORDER by id ASC";
  73. $result = $conn->query($sql);
  74.  
  75. echo "<div class='row'>";
  76. if ($result->num_rows > 0) {
  77. // output data of each row
  78. while ($product = $result->fetch_assoc()) {
  79. ?>
  80. <div class="col-sm-4 col-md-3">
  81. <form action="cart.php?action=add&id=<?php echo $product[ 'id' ]; ?>"
  82. method="post">
  83. <div class="products">
  84. <img class="img-responsive" src="<?php echo $product['image']; ?>"
  85. style="width:100%" />
  86. <h4 class="text-info">
  87. <?php echo $product['name']; ?>
  88. </h4>
  89. <h4> €
  90. <?php echo $product['price']; ?>
  91. </h4>
  92. <input type="text" name="quantity" class="form-control" value="1" style="width:25%" />
  93. <input type="hidden" name="name" value="<?php echo $product['name']; ?>" />
  94. <input type="hidden" name="price" value="<?php echo $product['price']; ?>" />
  95. <input type="submit" name="add_to_cart" class="btn btn-info" value="Add to Cart" />
  96. </div>
  97. </form>
  98. </div>
  99.  
  100. <?php
  101. // tole je var_dumb (pokaže kaj je napisano v mySql) -> print_r($product);
  102. }
  103. } else {
  104. echo "0 results";
  105. }
  106. echo "</div>";
  107.  
  108. $conn->close();
  109. ?>
  110.  
  111.  
  112.  
  113.  
  114. </body>
  115.  
  116. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement